ruby array
Array.select方法 (Array.select Method)
In the last articles, we have seen how to iterate over the instances of Array class? We have seen that we have got methods like Array.each, Array.reverse_each and Array.map for this purpose. In this article, we will learn about the implementation of Array.select.
在上一篇文章中,我們已經看到了如何遍歷Array類的實例? 我們已經看到,為此目的,我們已經有了Array.each , Array.reverse_each和Array.map之類的方法。 在本文中,我們將學習Array.select的實現。
Array.select method, as the name suggests, is used to select some elements from the Array. This method is non-destructive and does not bring any change in the actual values of the Array object. This method works based on certain conditions which you will provide inside the pair of parentheses. This method is based on the criteria you provide inside the block. This method will not work if you do not specify any conditions inside the block. Though it will not throw an exception you will get nil as the result. So, if you are working with this method you should be having certain conditions based on which the element is going to be selected from the object of Array class. If you want to print all the elements of Array instance then you can go for Array.each method and avoid going for this one.
顧名思義, Array.select方法用于從Array中選擇一些元素。 此方法是非破壞性的,不會對Array對象的實際值帶來任何變化。 該方法根據您將在圓括號內提供的某些條件來工作。 此方法基于您在塊內提供的條件。 如果未在塊內指定任何條件,則此方法將不起作用。 盡管它不會引發異常,但您將得到nil作為結果。 因此,如果使用此方法,則應具有某些條件,根據這些條件要從Array類的對象中選擇元素。 如果要打印Array實例的所有元素,則可以使用Array.each方法,并避免使用該方法 。
Syntax:
句法:
Array.select{|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 == 'a'
puts "Even numbers are:"
puts num.select{|num|
num%2 == 0
}
elsif opt == 'b'
puts "Odd numbers are:"
puts num.select{|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.select 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.
在上面的代碼中,您可以觀察到我們正在從用戶那里獲取用戶想要輸入什么類型的數字作為輸入。 這是因為我們要在Array.select方法內部傳遞某些條件。 我們根據用戶提供的選項向用戶提供響應,并且僅以這種方式使用此方法。
Example 2:
范例2:
=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 num.select{|a|}
Output
輸出量
# no o/p
Explanation:
說明:
In the above output, you can observe that when you are not specifying any condition inside the method, then you are not getting anything as the output.
在上面的輸出中,您可以觀察到,當您未在方法內部指定任何條件時,則不會得到任何輸出。
翻譯自: https://www.includehelp.com/ruby/array-select-method-with-example.aspx
ruby array