array.unshift
Array.unshift()方法 (Array.unshift() Method)
In this article, we will study about Array.unshift() Method. You all must be thinking the method must be doing something which is related to unshifting of 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.unshift()方法 。 你们都必须认为该方法必须执行与Array实例中对象的移位相关的操作。 它并不像看起来那么简单。 好吧,我们将在其余内容中解决这个问题。 我们将尝试借助语法并演示程序代码来理解它。
Method description:
方法说明:
This method is a public instance method and defined for the Array class in Ruby's library. This method works in a way that affixes the object which is provided with the method at the time of its invocation to the front of the self Array object. It simply means that the object will be shifted to the 0th index and the indices of rest objects are incremented by one. You can provide more than one argument to the method at the time of its invocation. This method is one of the examples of destructive methods where the changes created by the method are permanent. There is no non-destructive version of this method.
该方法是一个公共实例方法,为Ruby库中的Array类定义。 此方法的工作方式是在调用方法时将随方法提供的对象附加到self Array对象的前面。 这仅表示该对象将移至第0个索引,其余对象的索引将增加1。 调用方法时,可以为该方法提供多个参数。 此方法是破坏性方法的示例之一,该方法所创建的更改是永久性的。 没有此方法的非破坏性版本。
Syntax:
句法:
array_instance.unshift(object) -> array
Argument(s) required:
所需参数:
This method can take n number of objects. You can even provide an Array instance as the argument.
此方法可以使用n个对象。 您甚至可以提供一个Array实例作为参数。
Example 1:
范例1:
=begin
Ruby program to demonstrate unshift method
=end
# array declaration
table = [2,4,8,10,12,134,160,180,200,344]
puts "Array unshift implementation"
puts "Enter the number of objects you want to add:"
num = gets.chomp.to_i
for i in 1..num
puts "Enter the object:"
ele = gets.chomp
table.unshift(ele)
end
puts "The final Array instance: #{table}"
Output
输出量
Array unshift implementation
Enter the number of objects you want to add:
3
Enter the object:
Hrithik
Enter the object:
Amisha
Enter the object:
Satyam
The final Array instance: ["Satyam", "Amisha", "Hrithik", 2, 4, 8, 10, 12, 134, 160, 180, 200, 344]
Explanation:
说明:
In the above code, you can observe that we are adding elements in the Array instance with the help of the Array.unshift() method. In the output, you can see that the objects which are given by the user are stored from 0th index. The previously present elements are moved upward.
在上面的代码中,您可以观察到,借助于Array.unshift()方法 ,我们正在Array实例中添加元素。 在输出中,您可以看到用户指定的对象是从第 0 个索引开始存储的。 先前存在的元素向上移动。
Example 2:
范例2:
=begin
Ruby program to demonstrate unshift method
=end
# array declaration
table = [2,4,8,10,12,134,160,180,200,344]
puts "Array unshift implementation"
Name = ["Ayush","Saksham","Nikhil"]
table.unshift(Name)
puts "The final Array instance: #{table}"
Output
输出量
Array unshift implementation
The final Array instance: [["Ayush", "Saksham", "Nikhil"], 2, 4, 8, 10, 12, 134, 160, 180, 200, 344]
Explanation:
说明:
In the above code, you can observe that we have prepended an Array instance in the self Array. Now, it has become a subarray of the self Array which is stored at the 0th index of the self Array.
在上面的代码中,您可以观察到我们已经在self Array中添加了Array实例。 现在,它已成为self Array的子数组,存储在self Array的第0 个索引处。
翻译自: https://www.includehelp.com/ruby/array-unshift-method-with-example.aspx
array.unshift