ruby array_Ruby中带有示例的Array.cycle()方法
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