array.slice_Ruby中带有示例的Array.slice!()方法

array.slice

Array.slice!()方法 (Array.slice!() Method)

In this article, we will study about Array.slice!() method. You all must be thinking the method must be doing something which is related to slicing of elements or objects in the Array instance. It is not as simple as it looks. Well, we will figure this out in the rest of our content. We will try to understand it with the help of syntax and demonstrating program codes.

在本文中,我们将研究Array.slice!()方法 。 大家都必须认为该方法必须执行与Array实例中的元素或对象切片有关的操作。 它并不像看起来那么简单。 好吧,我们将在其余内容中解决这个问题。 我们将尝试借助语法并演示程序代码来理解它。

Method description:

方法说明:

This method is a public instance method and defined for the Array class in Ruby's library. This method works on element reference and returns the element at the index which is passed with the method invocation. If you are passing two parameters with the method and those parameters are the start and the length then the method will return a subarray which will contain the elements from the start index and till the length index. This method will return subarray in the case when the range is passed as the parameter at the time of method invocation. This method is one of the examples of destructive method where the method brings changes in the actual arrangement of objects in the self Array.

该方法是一个公共实例方法,为Ruby库中的Array类定义。 此方法在元素引用上起作用,并在与方法调用一起传递的索引处返回元素。 如果您通过方法传递两个参数,并且这些参数分别是开始和长度,则该方法将返回一个子数组,该子数组将包含从开始索引到长度索引的元素。 在方法调用时将范围作为参数传递的情况下,此方法将返回子数组。 此方法是破坏性方法的示例之一,该方法带来了自身Array中对象实际排列的更改。

Syntax:

句法:

    array_instance.slice!(index) -> object or nil
    or
    array_instance.slice!(start,length)-> new_array or nil
    or
    array_instance.slice!(range)-> new_array or nil

Argument(s) required:

所需参数:

You can provide a single index or range or start and length as the argument inside this method at the time of the method call. You will get the output on the basis of the argument you pass inside the method.

您可以在调用方法时在此方法内提供单个索引或范围,起始和长度作为参数。 您将根据在方法内部传递的参数获得输出。

Example 1:

范例1:

=begin
  Ruby program to demonstrate slice! method
=end

# array declaration
table = [2,4,6,8,10,12,14,16,18,20]

puts "Array slice! implementation"

puts "Enter the index you want to slice"
ind = gets.chomp.to_i

if(table.slice!(ind))
	puts "The element which is sliced is #{table.slice!(ind)}"
else
	puts "Array index out of bound"
end

puts "Array instance after slicing: #{table}"

Output

输出量

Array slice! implementation
Enter the index you want to slice
 4
The element which is sliced is 12
Array instance after slicing: [2, 4, 6, 8, 14, 16, 18, 20]

Explanation:

说明:

In the above code, you can observe that we are slicing the element from the Array instance with the help of Array.slice!() method. We are slicing it with the help of an index for which we have asked the user to the input value. The 4th index object has been sliced from the Array instance. The method is bringing changes in the self Array due to the fact that this method is one of the examples of destructive methods.

在上面的代码中,您可以观察到我们正在借助Array.slice!()方法从Array实例切片元素 。 我们正在使用一个索引对它进行切片,该索引已要求用户输入输入值。 第四索引对象已从Array实例中切出。 由于该方法是破坏性方法的示例之一,因此该方法使self Array发生了变化。

Example 2:

范例2:

=begin
  Ruby program to demonstrate slice! method
=end

# array declaration
table = [2,4,6,8,10,12,14,16,18,20]

puts "Array slice! implementation"

puts "Enter the start index you want to slice"
ind = gets.chomp.to_i

puts "Enter the length"
len = gets.chomp.to_i

if(table.slice(ind,len))
	puts "The sub array which is sliced is #{table.slice!(ind,len)}"
else
	puts "Array index out of bound"
end

puts "Array instance after slicing: #{table}"

Output

输出量

Array slice! implementation
Enter the start index you want to slice
 3
Enter the length
 5
The sub array which is sliced is [8, 10, 12, 14, 16]
Array instance after slicing: [2, 4, 6, 18, 20]

Explanation:

说明:

In the above code you can observe that we are creating a subarray from the elements of the Array instance with the help of Array.slice!() method. We are slicing it with the help of two parameters which are namely start index and length for which we have asked the user to input values. In the output, you can see that the Array instance has been sliced from the 3rd index and to the 7th index resulting in the formation of an Array which contains five objects. The method is bringing changes in the self Array due to the fact that this method is one of the examples of destructive method.

在上面的代码中,您可以观察到, 借助于Array.slice!()方法 ,我们正在根据Array实例的元素创建一个子数组 。 我们在两个参数的帮助下对其进行切片,即我们要求用户输入值的起始索引和长度。 在输出中,你可以看到,Array实例已经从第三索引,并导致其中包含五个对象的阵列的形成的7 索引切片。 由于该方法是破坏性方法的示例之一,因此该方法给self Array带来了变化。

Example 3:

范例3:

=begin
  Ruby program to demonstrate slice! method
=end

# array declaration
table = [2,4,6,8,10,12,14,16,18,20]

puts "Array slice! implementation"

puts "Enter the start index you want to slice"
ind = gets.chomp.to_i

puts "Enter the end index you want to slice"
len = gets.chomp.to_i

if(table.slice(ind..len))
	puts "The sub array which is sliced is #{table.slice!(ind..len)}"
else
	puts "Array index out of bound"
end

puts "Array instance after slicing: #{table}"

Output

输出量

Array slice! implementation
Enter the start index you want to slice
 3
Enter the end index you want to slice
 5
The sub array which is sliced is [8, 10, 12]
Array instance after slicing: [2, 4, 6, 14, 16, 18, 20]

Explanation:

说明:

In the above code, you can observe that we are slicing a subarray from the Array instance. We are slicing it with the help of a range for which we have asked the user to input values. In the output, you can see that the Array instance has been sliced from the 3rd index and to the 5th index resulting in the formation of an Array which contains three objects. The method is bringing changes in the self Array due to the fact that this method is one of the examples of the destructive method.

在上面的代码中,您可以观察到我们正在从Array实例中切片一个子数组。 我们在要求用户输入值的范围内对其进行切片。 在输出中,你可以看到,Array实例已经从第三索引,并导致其中包含三个对象的阵列的形成的5 索引切片。 由于该方法是破坏性方法的示例之一,因此该方法给self Array带来了变化。

翻译自: https://www.includehelp.com/ruby/array-slice-inverse-method-with-example.aspx

array.slice

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值