ruby中、.reject
Ruby Array.reject方法 (Ruby Array.reject Method)
In the last article, we have seen how we can make use of the Array.select method in order to print the Array elements based on certain conditions provided inside the block? In this article, we will see how we can make use of the Array.reject method? Array.reject method is totally opposite of the Array.select method.
在上一篇文章中,我們看到了如何利用Array.select方法來基于塊內提供的某些條件來打印Array元素? 在本文中,我們將看到如何利用Array.reject方法 ? Array.reject方法與Array.select方法完全相反。
Array.reject method, as the name suggests, is used to reject some elements from the Array. The elements will not be printed if they are satisfying the condition or criteria provided inside the block. This method is a non-destructive method and does not bring any change in the actual values of the Array object. This method works on the basis of certain conditions which you will provide inside the pair of parentheses. This method is totally based on the criteria you provide inside the block. This method will work even if you do not specify any conditions inside the block. It will print all the values if you are not providing any condition or criteria of rejection.
顧名思義, Array.reject方法用于拒絕Array中的某些元素。 如果元素滿足塊內提供的條件或標準,則不會打印這些元素。 此方法是一種非破壞性方法,不會對Array對象的實際值帶來任何變化。 該方法根據您將在一對括號內提供的某些條件來工作。 此方法完全基于您在塊內提供的條件。 即使您未在塊內指定任何條件,此方法也將起作用。 如果您未提供任何拒絕條件或條件,它將打印所有值。
Syntax:
句法:
Array.reject{|var| #condition}
Parameter(s):
參數:
This method does not permit the passing of any arguments instead it mandates a condition.
此方法不允許傳遞任何參數,而是要求一個條件。
Example 1:
范例1:
=begin
Ruby program to demonstrate Array.select
=end
# array declaration
num = [2,44,2,5,7,83,5,67,12,11,90,78,9]
puts "Enter 'a' for Even numbers and 'b' for odd numbers"
opt = gets.chomp
if opt == 'b'
puts "Odd numbers are:"
puts num.reject{|num|
num%2 == 0
}
elsif opt == 'a'
puts "Even numbers are:"
puts num.reject{|num|
num%2 !=0
}
else
puts "Wrong selection. Input valid option"
end
Output
輸出量
RUN 1:
Enter 'a' for Even numbers and 'b' for odd numbers
a
Even numbers are:
2
44
2
12
90
78
RUN 2:
Enter 'a' for Even numbers and 'b' for odd numbers
b
Odd numbers are:
5
7
83
5
67
11
9
Explanation:
說明:
In the above code, you can observe that we are taking input from the user about what type of numbers the user wants as the output. This is because we want to pass certain conditions inside the Array.reject method. We are giving the response to the user as per the option provided by the user and this method is used in this way only. It is rejecting the elements which are satisfying the condition provided inside the block.
在上面的代碼中,您可以觀察到我們正在從用戶那里獲取用戶想要輸入什么類型的數字作為輸入。 這是因為我們要在Array.reject方法內部傳遞某些條件。 我們根據用戶提供的選項向用戶提供響應,并且僅以這種方式使用此方法。 它拒絕滿足塊內提供的條件的元素。
Example 2:
范例2:
=begin
Ruby program to demonstrate Array.reject
=end
# array declaration
num = [2,44,2,5,7,83,5,67,12,11,90,78,9]
puts num.reject{|a|}
Output
輸出量
2
44
2
5
7
83
5
67
12
11
90
78
9
Explanation:
說明:
In the above output, you can observe that when you are not specifying any condition inside the method, it is still not throwing any kind of exception inside it will print all the elements present in the Array instance. This is one more point by which we can differentiate Array.select and Array.reject.
在上面的輸出中,您可以觀察到,當您在方法內未指定任何條件時,它仍未引發任何異常,它將打印Array實例中存在的所有元素。 這是我們可以區分Array.select和Array.reject的另一點。
翻譯自: https://www.includehelp.com/ruby/array-reject-method-with-example.aspx
ruby中、.reject