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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

ruby中、.reject_Ruby中带有示例的Array.reject方法

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

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:

=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.chompif 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} 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.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:

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

總結

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

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