ruby array
Array.shift方法 (Array.shift Method)
In this article, we will study about Array.shift method. You all must be thinking the method must be doing something which is related to shifting 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.shift方法 。 大家都必须认为该方法必须执行与Array实例中元素或对象的移位有关的操作。 它并不像看起来那么简单。 好吧,我们将在其余内容中解决这个问题。 我们将尝试借助语法并演示程序代码来理解它。
Method description:
方法说明:
This method is a public instance method and defined for the Array class in Ruby's library. This method works in such a way that if no argument is provided then it removes the first element from the Array instance and returns that Array instance. If you are providing an integer argument 'n' to this method, then it will remove first 'n' elements from the Array instance and returns a new Array instance which will contain those 'n' elements which are removed from the Array instance. This method is one of the examples of destructive methods where changes created by the method are permanent and directly affect the original arrangement of objects in the Array object.
该方法是一个公共实例方法,为Ruby库中的Array类定义。 此方法的工作方式是,如果未提供任何参数,则它将从Array实例中删除第一个元素并返回该Array实例。 如果为此方法提供整数参数'n' ,则它将从Array实例中删除前一个'n'元素,并返回一个新的Array实例,其中将包含从Array实例中删除的那些'n'元素。 此方法是破坏性方法的示例之一,该方法所产生的更改是永久性的,并且会直接影响Array对象中对象的原始排列。
Syntax:
句法:
array_instance.shift -> object or nil
or
array_instance.shift(n)-> new_array
Argument(s) required:
所需参数:
This method takes one argument which decides the length of the Array instance which is returned by the method.
此方法采用一个参数,该参数确定该方法返回的Array实例的长度。
Example 1:
范例1:
=begin
Ruby program to demonstrate shift method
=end
# array declaration
table = [2,4,6,8,10,12,14,16,18,20]
puts "Array shift implementation"
pq =table.shift
puts "The element which is removed is #{pq}"
puts "Array instance:"
print table
Output
输出量
Array shift implementation
The element which is removed is 2
Array instance:
[4, 6, 8, 10, 12, 14, 16, 18, 20]
Explanation:
说明:
In the above code, you can observe that we are shifting or removing the elements from the Array instance with the help of Array.shift method. You can observe that the Array instance is no more having an object which was residing on the 0th index because we have invoked Array.shift method along with the Array instance.
在上面的代码中,您可以观察到借助于Array.shift方法 ,我们正在从Array实例中移动或删除元素 。 您可以观察到Array实例不再具有驻留在第0 个索引上的对象,因为我们已经与Array实例一起调用了Array.shift方法 。
Example 2:
范例2:
=begin
Ruby program to demonstrate shift method
=end
# array declaration
table = [2,4,6,8,10,12,14,16,18,20]
puts "Array shift implementation"
puts "Enter the number of objects you want to shift:"
num = gets.chomp.to_i
rn = table.shift(num)
puts "The objects shifted from Array instance is #{rn}"
puts "Array instance: #{table}"
Output
输出量
Array shift implementation
Enter the number of objects you want to shift:
3
The objects shifted from Array instance is [2, 4, 6]
Array instance: [8, 10, 12, 14, 16, 18, 20]
Explanation:
说明:
In the above code, you can observe that we are shifting or removing the elements from the Array instance with the help of Array.shift method. You can observe that the Array instance is no more having objects which were residing on the 0th, 1st, and 2nd indices because we have invoked Array.shift method along with the Array instance. The method is returning a new Array instance.
在上面的代码中,您可以观察到借助于Array.shift方法 ,我们正在从Array实例中移动或删除元素 。 你可以观察到Array实例不再具有被驻留在0 次 ,1 次和2 次指数,因为我们与Array实例调用一起的方法Array.shift对象。 该方法返回一个新的Array实例。
翻译自: https://www.includehelp.com/ruby/array-shift-method-with-example.aspx
ruby array