欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ruby array_Ruby中带有示例的Array.drop_while方法

程序员文章站 2024-02-14 17:19:28
...

ruby array

Ruby Array.drop_while方法 (Ruby Array.drop_while Method)

In the last articles, we have studied about Array.select and Array.reject methods which give output to the user based on certain conditions. The method Array.drop_while is also a non-destructive method.

在上一篇文章中,我们研究了Array.selectArray.reject方法,这些方法根据特定条件将输出提供给用户。 Array.drop_while方法也是一种非破坏性方法。

Method description:

方法说明:

Array.drop_while method works similarly to a loop. As the method, itself contains the word "while" which makes us think that this method is related to a while loop. The operation is just the opposite of while loop. While, while loop works as it processes the things until the condition is not falsified, Array.drop_while method discards or drops the elements until it does not find the element which is specified inside the block. It will print every element if it does not find the element in the Array instance which is defined inside the block of the method. If the specified element lies between two elements of the Array instance, then it will start printing from the greatest one. This method does not through an exception in such a case. It simply drops the elements until it does not find the defined element.

Array.drop_while方法的工作原理类似于循环。 作为方法,它本身包含单词“ while” ,这使我们认为该方法与while循环有关 。 该操作与while循环相反。 虽然while循环在处理事物之前一直起作用,直到条件不被伪造为止,但Array.drop_while方法丢弃或丢弃元素,直到找不到在块内指定的元素为止。 如果找不到在方法块内定义的Array实例中的元素,它将打印每个元素。 如果指定的元素位于Array实例的两个元素之间,则它将从最大的元素开始打印。 在这种情况下,此方法不会出现异常。 它只是删除元素,直到找不到定义的元素。

Syntax:

句法:

    Array.drop_while{|var| #condition}

Parameter(s): This method does not accept any parameter instead a Boolean condition is required inside the block.

参数:该方法不接受任何参数,而是在块内需要布尔条件。

Example 1:

范例1:

=begin
    Ruby program to demonstrate Array.drop_while
=end

# array declaration
num = [1,2,3,4,5,6,7,8,9,10,23,11,33,55,66,12]

# user input
puts "Enter the element after which you want to see the result"
lm = gets.chomp.to_i

flag = false
num.each {|nm|
if nm == lm
	flag = true
end
}
if flag == true
    puts "Elements after #{lm} are:"
    puts num.drop_while { |a| a < lm }
else
	puts "Element not found"
end

Output

输出量

Enter the element after which you want to see the result
5
Elements after 5 are:
5
6
7
8
9
10
23
11
33
55
66
12

Explanation:

说明:

In the above code, you can see that this method requires a sequenced Array of elements. It has printed all the elements which are lying after the element 10. Here, first, we have checked the presence of the element in the Array with the help of Array.each then we have carried out the further processing.

在上面的代码中,您可以看到此方法需要一个元素顺序数组。 它已打印的所有这些元件10.这里后躺在元件中,首先,我们已经检查了元件的存在与的帮助下阵列Array.each然后我们已经进行了进一步的处理。

Example 2:

范例2:

=begin
    Ruby program to demonstrate Array.drop_while
=end

# array declaration
num = [1,2,3,4,5,6,7,8,9,10,23,11,33,55,66,12]

print num.drop_while{|a|}

Output

输出量

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 23, 11, 33, 55, 66, 12]

Explanation:

说明:

In the above code, you can observe that when you are not specifying any condition inside the method then it is printing every element of the Array object.

在上面的代码中,您可以观察到,当您在方法内未指定任何条件时,它将打印Array对象的每个元素。

翻译自: https://www.includehelp.com/ruby/array-drop_while-method-with-example.aspx

ruby array