ruby array
Array.take_while方法 (Array.take_while Method)
In this article, we will study about Array.take_while method. You all must be thinking the method must be doing something which is related to taking elements or objects from 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.take_while方法 。 你们都必须认为该方法必须在做一些与从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 it keeps on passing the objects to the block until the block returns false or nil. When the block returns false or nil, it stops the iteration and returns a new Array instance which contains all previous objects. If you are not providing any block or you are invoking the method without any block, then the method will return an enumerator.
该方法是一个公共实例方法,为Ruby库中的Array类定义。 此方法的工作方式是继续将对象传递给该块,直到该块返回false或nil。 当该块返回false或nil时,它将停止迭代并返回一个包含所有先前对象的新Array实例 。 如果不提供任何块,或者调用的方法没有任何块,则该方法将返回一个枚举器。
Syntax:
句法:
array_instance.take_while -> Enumerator
or
array_instance.take_while{|arr| block}-> new_array or nil
Argument(s) required:
所需参数:
This method does not take any argument. Instead you will have to pass a block.
此方法不带任何参数。 相反,您将必须通过一个块。
Example 1:
范例1:
=begin
Ruby program to demonstrate take_while method
=end
# array declaration
table = [2,4,8,10,12,134,160,180,200]
puts "Array take_while implementation"
puts "Enter the maximum limit:"
num = gets.chomp.to_i
rslt = table.take_while{|i|i<num}
puts "The Array instance returned from the method is:#{rslt}"
Output
输出量
Array take_while implementation
Enter the maximum limit:
34
The Array instance returned from the method is:[2, 4, 8, 10, 12]
Explanation:
说明:
In the above code, you can observe that we are taking elements from the Array instance with the help of Array.take_while method. We have asked the user for the maximum number and the method is returning true for all the integers which are less than the maximum number. In the end, the Array object which is returned has all the prior elements for which the method has returned true.
在上面的代码中,您可以观察到我们是在Array.take_while方法的帮助下从Array实例中获取元素的。 我们已经要求用户提供最大数量,并且该方法对于所有小于最大数量的整数都返回true。 最后,返回的Array对象具有该方法已返回true的所有先前元素。
Example 2:
范例2:
=begin
Ruby program to demonstrate take_while method
=end
# array declaration
table = [1,3,4,6,7,8,19,21,22,24,25,99]
puts "Array take_while implementation"
puts table.take_while
Output
输出量
Array take_while implementation
#<Enumerator:0x0000561a367d3718>
Explanation:
说明:
In the above code, you can observe that the method Array.take_while is returning an enumerator when we are invoking or calling it without any block.
在上面的代码中,您可以观察到,当我们调用或调用它而没有任何块时, Array.take_while方法正在返回一个枚举数。
翻译自: https://www.includehelp.com/ruby/array-take_while-method-with-example.aspx
ruby array