日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/1 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ruby array_Ruby中带有示例的Array.select方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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:

=beginRuby 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} elseputs "Wrong selection. Input valid option" end

Output

輸出量

RUN 1: Enter 'a' for Even numbers and 'b' for odd numbersa Even numbers are: 2 44 2 12 90 78RUN 2: Enter 'a' for Even numbers and 'b' for odd numbersb 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:

=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 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

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的ruby array_Ruby中带有示例的Array.select方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。