ruby array_Array.select! Ruby中的示例方法
ruby array
Array.select! 方法 (Array.select! Method)
In this article, we will study about Array.select! Method. You all must be thinking the method must be doing something related to the selection of 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.select! 方法 。 你們都必須認(rèn)為該方法必須在做一些與從Array實(shí)例中選擇對(duì)象有關(guān)的事情。 它并不像看起來(lái)那么簡(jiǎn)單。 好吧,我們將在其余內(nèi)容中解決這個(gè)問(wèn)題。 我們將嘗試借助語(yǔ)法并演示程序代碼來(lái)理解它。
Method description:
方法說(shuō)明:
This method is a public instance method and defined for the Array class in Ruby's library. This method, as the name suggests, is used to select some elements from the Array. This method is destructive and brings changes in the actual values of the Array object. This method works based on a certain condition which you will provide inside the pair of parenthesis. This method is based on the criteria you provide inside the block. This method will not work if you do not specify any condition 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 a certain condition 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.
該方法是一個(gè)公共實(shí)例方法,為Ruby庫(kù)中的Array類定義。 顧名思義,此方法用于從數(shù)組中選擇一些元素。 此方法具有破壞性,并會(huì)更改Array對(duì)象的實(shí)際值。 此方法基于您將在圓括號(hào)內(nèi)提供的特定條件。 此方法基于您在塊內(nèi)提供的條件。 如果未在塊內(nèi)指定任何條件,則此方法將不起作用。 盡管它不會(huì)引發(fā)異常,但您將得到nil作為結(jié)果。 因此,如果您正在使用此方法,則應(yīng)該具有一定的條件,根據(jù)該條件要從Array類的對(duì)象中選擇元素。 如果要打印Array實(shí)例的所有元素,則可以使用Array.each方法,并避免使用該方法。
Syntax:
句法:
array.select!{|var| #condition}Argument(s) required:
所需參數(shù):
This method does not permit the passing of any arguments instead it mandates a condition.
此方法不允許傳遞任何參數(shù),而是要求一個(gè)條件。
Example 1:
范例1:
=beginRuby program to demonstrate Array.select! method =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.chompif opt == 'a'puts "Even numbers are:"puts num.select!{|num|num%2 == 0}puts "Array instance: #{num}" elsif opt == 'b'puts "Odd numbers are:"puts num.select!{|num|num%2 !=0}puts "Array instance: #{num}" elseputs "Wrong selection. Input valid option" endOutput
輸出量
RUN 1: Enter 'a' for Even numbers and 'b' for odd numbersa Even numbers are: 2 44 2 12 90 78 Array instance: [2, 44, 2, 12, 90, 78]RUN 2: nter 'a' for Even numbers and 'b' for odd numbersb Odd numbers are: 5 7 83 5 67 11 9 Array instance: [5, 7, 83, 5, 67, 11, 9]Explanation:
說(shuō)明:
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 responses to the user as per the option provided by the user and this method is used in this way only. You can see that this method is creating a direct impact on the self Array instance because this method is one of the examples if destructive methods.
在上面的代碼中,您可以觀察到我們正在從用戶那里獲取用戶想要輸入什么類型的數(shù)字作為輸入。 這是因?yàn)槲覀円?strong>Array.select內(nèi)部傳遞某些條件! 方法 。 我們根據(jù)用戶提供的選項(xiàng)向用戶提供響應(yīng),并且僅以這種方式使用此方法。 您可以看到此方法對(duì)self Array實(shí)例產(chǎn)生直接影響,因?yàn)樵摲椒ㄊ瞧茐男苑椒ǖ氖纠弧?
Example 2:
范例2:
=beginRuby 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 Output.Explanation:
說(shuō)明:
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.
在上面的輸出中,您可以觀察到,當(dāng)您未在方法內(nèi)部指定任何條件時(shí),則不會(huì)得到任何輸出。
翻譯自: https://www.includehelp.com/ruby/array-select-inverse-method-with-example.aspx
ruby array
總結(jié)
以上是生活随笔為你收集整理的ruby array_Array.select! Ruby中的示例方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python 向量取整数_随机整数向量|
- 下一篇: java uuid静态方法_Java U