ruby array_Ruby中带有示例的Array.rotate()方法
ruby array
Ruby Array.rotate()方法 (Ruby Array.rotate() Method)
In this article, we will study about Array.rotate() method. You all must be thinking the method must be doing something related to rotating certain elements. 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.rotate()方法 。 大家都必须认为该方法必须执行与旋转某些元素相关的操作。 它并不像看起来那么简单。 好吧,我们将在其余内容中解决这个问题。 我们将尝试借助语法并演示程序代码来理解它。
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 it rotates the content or objects present inside the Array instances. It rotates the Array elements in the way that the second element is considered to be first and the last object is considered to be the first element of the object of Array class. If you pass any negative integer inside the method then the rotation is done in the opposite direction. Array.rotate() is a non-destructive method where the changes created by this method would not impact the actual order of the Self Array instance and these changes are not permanent.
该方法是一个公共实例方法,为Ruby库中的Array类定义。 该方法的工作方式是旋转Array实例中存在的内容或对象。 它以以下方式旋转Array元素:将第二个元素视为第一个元素,将最后一个对象视为Array类的对象的第一个元素。 如果在方法内部传递任何负整数,则旋转方向相反。 Array.rotate()是一种非破坏性方法,其中此方法创建的更改不会影响Self Array实例的实际顺序,并且这些更改不是永久的。
Syntax:
句法:
Array_instance.rotate -> new_array
or
Array_instance.rotate(count) -> new_array
Argument(s) required:
所需参数:
This method does takes one argument and that argument decides from which index the rotation is going to be held.
此方法确实接受一个参数,并且该参数决定将从哪个索引中保留旋转。
Example 1:
范例1:
=begin
Ruby program to demonstrate rotate method
=end
# array declaration
Lang = ["C++","Java","Python","Html","Javascript","php","Ruby","Kotlin"]
puts "Array rotate implementation."
print Lang.rotate
puts ""
puts "The first element of the Array is: #{Lang[0]}"
puts "Array elements are:"
print Lang
Output
输出量
Array rotate implementation.
["Java", "Python", "Html", "Javascript", "php", "Ruby", "Kotlin", "C++"]
The first element of the Array is: C++
Array elements are:
["C++", "Java", "Python", "Html", "Javascript", "php", "Ruby", "Kotlin"]
Explanation:
说明:
In the above code, you can observe that we are rotating the contents of the Array class instance with the help of Array.rotate() method. You can observe that after rotating the contents, the first element is "Java" and the last element is "C++". Due to the fact that this method is a non-destructive method, it is not creating any impact on the actual arrangements of elements in the Array instance.
在上面的代码中,您可以观察到借助于Array.rotate()方法 ,我们正在旋转Array类实例的内容。 您可以观察到旋转内容之后,第一个元素是“ Java” ,最后一个元素是“ C ++” 。 由于此方法是一种非破坏性方法,因此它不会对Array实例中元素的实际排列产生任何影响。
Example 2:
范例2:
=begin
Ruby program to demonstrate rotate method
=end
# array declaration
Table = [2,4,6,8,10,12,14,16,18,20]
puts "Array rotate implementation"
print Table.rotate(-2)
puts ""
puts "The first element of the Array is: #{Table.first}"
puts "Array elements are:"
print Table
Output
输出量
Array rotate implementation
[18, 20, 2, 4, 6, 8, 10, 12, 14, 16]
The first element of the Array is: 2
Array elements are:
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
Explanation:
说明:
In the above code, you can observe that this method works on Integer Array as well and we are rotating the contents of Array class instance with the help of Array.rotate() method. Since we are passing a negative integer inside the method, the rotation is started from the back end side of the Array instance and the 2nd element from the last. You can observe that after rotating the contents, the first element is still 2 because this method is non-destructive and does not create any changes in the actual arrangements of contents in the Array instance.
在上面的代码中,您可以观察到该方法也适用于Integer Array,并且借助于Array.rotate()方法 ,我们正在旋转Array类实例的内容。 因为我们在方法内部传递了一个负整数,所以旋转从Array实例的后端开始,而第二个元素从最后一个开始。 您可以观察到,在旋转内容之后,第一个元素仍为2,因为此方法是非破坏性的,并且不会在Array实例中对内容的实际排列进行任何更改。
翻译自: https://www.includehelp.com/ruby/array-rotate-method-with-example.aspx
ruby array