ruby array
Array.cycle()方法 (Array.cycle() Method)
In this article, we will study about Array.cycle() method. You must be a little more excited to read about Array.cycle method due to its catchy name as I was pretty amazed after reading this method. In the upcoming content, we will see the way through which we can implement this method. We will understand it with the help of its syntax and demonstrating examples.
在本文中,我們將研究Array.cycle()方法 。 由于Array.cycle方法具有易記的名稱,因此您必須更加興奮地閱讀Array.cycle方法,因為閱讀此方法后我感到非常驚訝。 在接下來的內容中,我們將看到實現此方法的方式。 我們將借助其語法和演示示例來理解它。
Method description:
方法說明:
This method is specially defined for the Array class in Ruby's library. This method is one of the examples of Array instance methods. It works like a loop and calls or invokes the given block for each element of Array instance for n number of times. This method will not work if you do not provide any positive number. If you are not providing anything then it may result in the infinite repetition of the elements of the object of Array class. The return type of this method is "nil" and it does not undergo any change in the actual elements of Array objects. This method can be considered as one of the examples of non-destructive methods present in the Rich Ruby library.
此方法是為Ruby庫中的Array類專門定義的。 此方法是Array實例方法的示例之一。 它像循環一樣工作,并為Array實例的每個元素調用或調用給定的塊n次。 如果您不提供任何正數,則此方法將不起作用。 如果您不提供任何內容,則可能導致Array類對象的元素無限重復。 此方法的返回類型為“ nil”,并且在Array對象的實際元素中未進行任何更改。 該方法可以視為Rich Ruby庫中存在的非破壞性方法的示例之一。
Syntax:
句法:
cycle(n=nil) { |obj| block } -> nil
Argument(s) required:
所需參數:
This method requires one positive number and if nothing is passed then you have to encounter an infinite loop.
此方法需要一個正數,如果不進行任何傳遞,則必須遇到無限循環。
Example 1:
范例1:
=begin
Ruby program to demonstrate cycle method
=end
# array declaration
array1 = ["1","Ramesh","Apple","12","Sana","Yogita","Satyam","Harish"]
puts "Array cycle implementation."
array1.cycle(3){|x| puts x}
Output
輸出量
Array cycle implementation.
1
Ramesh
Apple
12
Sana
Yogita
Satyam
Harish
1
Ramesh
Apple
12
Sana
Yogita
Satyam
Harish
1
Ramesh
Apple
12
Sana
Yogita
Satyam
Harish
Explanation:
說明:
In the above code, you will observe that each element of the Array instance have been repeated three times and the repetition is not random, it is going in a proper sequence as stored inside the instance of Array class with which the method is invoked.
在上面的代碼中,您將觀察到Array實例的每個元素已經重復了3次,并且重復不是隨機的,它按照正確的順序存儲在調用該方法的Array類實例中。
Example 2:
范例2:
=begin
Ruby program to demonstrate cycle method
=end
# array declaration
array1 = ["1","Ramesh","Apple","12","Sana","Yogita","Satyam","Harish"]
puts "Array cycle implementation."
array1.cycle{|x| puts x}
Output
輸出量
Array cycle implementation.
1
Ramesh
Apple
12
Sana
Yogita
Satyam
Harish
1
Ramesh
Apple
12
Sana
Yogita
Satyam
Harish
1
Ramesh
Apple
12
Sana
Yogita
Satyam
Harish
1
Ramesh
Apple
12
.
.
.
.
Infinite loop...
Explanation:
說明:
When you will run the above code, you will observe that it will result in an infinite loop. This has happened because you are not passing any argument or positive number with the method.
當您運行上述代碼時,您會發現它將導致無限循環。 發生這種情況是因為您沒有使用該方法傳遞任何參數或正數。
翻譯自: https://www.includehelp.com/ruby/array-cycle-method-with-example.aspx
ruby array