ruby array
Ruby Array.keep_if方法 (Ruby Array.keep_if Method)
In the last articles, we have studied the Array methods namely Array.select, Array.reject and Array.drop_while, all these methods are non–destructive methods which means that they do not impose any changes in the actual values of elements residing in the Array instance. If you want to make the above methods destructive, you can add an “!” after the method name. For instance, Array.select! is the destructive version of Array.select. We have also learned about Array.delete_if the method which is already destructive by nature.
在上一篇文章中,我们研究了Array方法,即Array.select , Array.reject和Array.drop_while ,所有这些方法都是非破坏性的方法,这意味着它们不对驻留在数组中的元素的实际值施加任何更改。数组实例。 如果要使上述方法具有破坏性,则可以添加“!” 方法名称之后。 例如,Array.select! 是Array.select的破坏性版本。 我们还了解了Array.delete_if该方法本质上已经具有破坏性。
In this article, we will learn about the Array method Array.keep_if which is also destructive by nature like the delete_if method.
在本文中,我们将学习Array方法Array.keep_if ,它与delete_if方法一样也具有破坏性。
Method description:
方法说明:
This method is used to select elements from the instance of the Array class at the same instant. This method is just opposite of Array.delete_if method because Array.delete_if method deletes the elements from the Array which do not satisfy the condition provided inside the block on the other hand Array.keep_if method keeps or saves the elements which satisfy the condition specified inside the block of the method. This method will remove all the elements from the Array instance if you do not specify or provide any condition inside the block.
此方法用于同时从Array类的实例中选择元素。 此方法与Array.delete_if方法恰好相反,因为Array.delete_if方法从Array中删除不满足块内部提供的条件的元素,而Array.keep_if方法则保留或保存满足内部指定条件的元素方法的块。 如果您未在块内指定或提供任何条件,则此方法将从Array实例中删除所有元素。
Syntax:
句法:
Array.keep_if{|var|#condition}
Parameter(s): This method does not accept any arguments instead it requires a Boolean condition for operation.
参数 :此方法不接受任何参数,而是需要布尔条件进行操作。
Example 1:
范例1:
=begin
Ruby program to demonstrate Array.keep_if
=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 your choice (a)keep even numbers (b) keep odd numbers"
lm = gets.chomp
if lm == 'a'
puts "Even numbers are:"
print num.keep_if { |a| a % 2 ==0 }
elsif lm == 'b'
puts "Odd numbers are:"
print num.keep_if { |a| a % 2 !=0 }
else
puts "Invalid Input"
end
Output
输出量
RUN 1:
Enter the your choice (a)keep even numbers (b) keep odd numbers
a
Even numbers are:
[2, 4, 6, 8, 10, 66, 12]
RUN 2:
Enter the your choice (a)keep even numbers (b) keep odd numbers
b
Odd numbers are:
[1, 3, 5, 7, 9, 23, 11, 33, 55]
Explanation:
说明:
In the above code, you can observe that the method is keeping all the elements that are satisfying the condition provided inside the block of Array.keep_if method. If the user is asking to keep odd numbers, then output is shown in RUN 2 and when the user is asking for even numbers, the output is shown in RUN 1.
在上面的代码中,您可以观察到该方法将保留所有满足Array.keep_if方法块中提供的条件的元素。 如果用户要求保留奇数,则在RUN 2中显示输出,而当用户要求偶数时,在RUN 1中显示输出。
Example 2:
范例2:
=begin
Ruby program to demonstrate Array.keep_if
=end
# array declaration
num = [1,2,3,4,5,6,7,8,9,10,23,11,33,55,66,12]
print num.keep_if{|a|}
puts ""
print num
Output
输出量
[]
[]
Explanation:
说明:
In the above code, you can observe that if you are not specifying any condition inside the method block, then it is deleting or removing all the elements from the Array instance.
在上面的代码中,您可以观察到,如果您未在方法块内指定任何条件,那么它将在Array实例中删除或删除所有元素。
翻译自: https://www.includehelp.com/ruby/array-keep_if-method-with-example.aspx
ruby array