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:

=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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/541030.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/541030.shtml
英文地址,請注明出處:http://en.pswp.cn/news/541030.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

java獲取主機mac_Java 如何獲取主機的MAC地址

獲取MAC地址首先要理解當前的操作系統,由于在不同的操作系統中CMD命令所在的位置不同,因此首先使用System類中的getProperty("os.name")方法獲取當前的操作系統,getProperty()方法可以確定當前系統屬性,它的參數是一些固…

微軟免費軟件項目DreamSpark更名為Microsoft Imagine

9月10日消息,微軟免費軟件項目DreamSpark近日正式更名為Microsoft Imagine,將與一年一度的微軟“創新杯(Imagine Cup)”齊名。微軟免費軟件項目DreamSpark更名為Microsoft Imagine  2008年2月19日,微軟公司董事長比爾蓋茨在斯坦福大學發布了…

java jpa_Java JPA 語法知識

前提操作創建一個可持久化的實體類dao層繼承JpaRepositoryT:實體類ID:實體類的主鍵類型例:public interface SysUserRespository extends JpaRepository {}JPA中支持的關鍵詞And --- 等價于 SQL 中的 and 關鍵字,比如 findByUsern…

array.slice_Ruby中帶有示例的Array.slice()方法

array.sliceArray.slice()方法 (Array.slice() Method) In this article, we will study about Array.slice() method. You all must be thinking the method must be doing something which is related to the slicing of elements or objects in the Array instance. It is n…

阿特斯攜手EDF啟動建設巴西191.5MW光伏項目

2016年10月11日,阿特斯太陽能(安大略省,圭爾夫)和EDF Energies Nouvelles(法國,巴黎)共同宣布,將阿特斯巴西Pirapora I太陽能項目80%的股權出售給EDF的巴西本地子公司EDF…

apachejmeter_java源碼_自定義編寫jmeter的Java測試代碼

我們在做性能測試時,有時需要自己編寫測試腳本,很多測試工具都支持自定義編寫測試腳本,比如LoadRunner就有很多自定義腳本的協議,比如"C Vuser","JavaVuser"等協議.同樣,Jmeter也支持自定義編寫的測試代碼,不過與LoadRunner不同的是,Jmeter沒有自帶編譯器,…

julia fit 函數_帶有Julia中示例的flipsign()函數

julia fit 函數Julia| flipsign()函數 (Julia | flipsign() function) flipsign() function is a library function in Julia programming language, it accepts two values as parameters and returns a value with the magnitude of first value and sign of the first value…

優化Android應用內存的若干方法

https://my.oschina.net/chaselinfo/blog/198172摘要: 在app開發的各個階段中要考慮RAM的限制問題, 包括在設計階段(正式開發之前). 使用下面的不同的方法可以達到很好的效果. 當您在設計和開發Android應用時用下面的方法可以使內存運用最高效.使用保守的Service 如果你的應用需…

一? ilkkn.n_IL&FS的完整形式是什么?

一? il&kkn.nIL&FS:基礎設施租賃和金融服務 (IL& FS: Infrastructure Leasing & Financial Services) IL&FS is an abbreviation of Infrastructure Leasing & Financial Services. It is the largest infrastructure development …

java notify喚醒原理_Java wait和notify虛假喚醒原理

自己在此記錄一下,方便日后復習。虛假喚醒的概念jdk官方文檔解釋:所以說在wait和notify一塊使用時,如果使用if作為條件時,會有虛假喚醒的情況發生,所以必須使用while作為循環條件。下面來舉例實驗:首先&…

C#里面的三種定時計時器:Timer

在.NET中有三種計時器:1、System.Windows.Forms命名空間下的Timer控件,它直接繼承自Componet。Timer控件只有綁定了Tick事件和設置EnabledTrue后才會自動計時,停止計時可以用Stop()方法控制,通過Stop()停止之后,如果想…

wireshark rto_RTO的完整形式是什么?

wireshark rtoRTO:地區運輸辦公室/公路運輸辦公室 (RTO: Regional Transport Office/ Road Transport Office) RTO is an abbreviation of the Regional Transport Office. It is an Indian Government departmental organization that is responsible for upholdin…

java8 json轉xml_2019-08-17java對象與json,xml互轉

依賴的jar包,jackson-all-1.7.6.jar,xstream-1.4.4.jar下載地址:鏈接:https://pan.baidu.com/s/1LflD135qlQiIPGXw5XwDmw提取碼:6v29復制這段內容后打開百度網盤手機App,操作更方便哦package json_xml;import com.thoughtworks.xs…

10.8-全棧Java筆記:序列化/反序列化的步驟和實例

本節我們詳細講解10.3節中提到的序列化和反序列化操作。序列化和反序列化是什么當兩個進程遠程通信時,彼此可以發送各種類型的數據。 無論是何種類型的數據,都會以二進制序列的形式在網絡上傳送。比如,我們可以通過http協議發送字符串信息&am…

有效的網絡推廣超級實用方法

我叫龍雨,先后在百度搜狗工作過3年,后來一直負責一家公司的的網絡營銷!不知道大家有沒有聽過111>3這樣一個概念,簡單來說一下這概念!第一呢就是自己的資源,把自己的資源維護好開發好;第二就是網絡營銷,網絡營銷利用…

什么為java運行時的環境_什么是JRE?Java運行時環境簡介(一)

Java開發工具包(JDK),Java虛擬機(JVM)和Java運行時環境(JRE)共同構成了用于開發和運行Java應用程序的Java平臺組件的強大功能.實際上,運行時環境是一種旨在運行其他軟件的軟件.作為Java的運行時環境,JRE包含Java類庫,Java類加載器和Java虛擬機.在這個系統中:的類加載器是負責正…

c語言atoll函數怎么用_C ++中帶有示例的atoll()函數

c語言atoll函數怎么用C Atoll()函數 (C atoll() function) atoll() function is a library function of cstdlib header. It is used to convert the given string value to the integer value. It accepts a string containing an integer (integral) number and returns its…

看清美國“黑客帝國”的真面目

“維基揭秘”網站近日發布了近9000份據稱屬于美國中央情報局的機密文件,顯示中情局擁有強大的黑客攻擊能力,秘密侵入了手機、電腦、智能電視等眾多智能設備。繼美國國家安全局承包商前雇員斯諾登曝光國安局“棱鏡”等監控計劃之后,此次曝光再…

python 示例_帶有示例的Python File close()方法

python 示例文件close()方法 (File close() Method) close() method is an inbuilt method in Python, it is used to flush and close the IO object (file). If we close a closed file it has no effect. close()方法是Python中的內置方法,用于刷新和關閉IO對象(…

linux上mysql分區磁盤位置_Linux下Oracle軟件、數據文件等所在的磁盤分區空間不足的解決思路...

虛擬機中的ORACLE運行的久了,歸檔、數據文件不斷增長,原來安裝ORACLE的分區空間不足。此時可以重新向虛擬機增加一塊硬盤,將OR虛擬機中的Oracle運行的久了,歸檔、數據文件不斷增長,原來安裝ORACLE的分區空間不足。此時…