ruby hash方法_Ruby中帶有示例的Hash.rassoc(obj)方法

ruby hash方法

Hash.rassoc(obj)方法 (Hash.rassoc(obj) Method)

In this article, we will study about Hash.rassoc(obj) Method. The working of the method can’t be assumed because of it’s quite a different name. Let us read its definition and understand its implementation with the help of syntax and program codes.

在本文中,我們將研究Hash.rassoc(obj)方法 。 由于該方法的名稱完全不同,因此無法進行假設。 讓我們閱讀其定義并在語法和程序代碼的幫助下了解其實現。

Method description:

方法說明:

This method is a Public instance method and belongs to the Hash class which lives inside the library of Ruby language. The Hash.rassoc() method is used to check whether a value(key-value) is a part of the particular Hash instance or not and that Hash instance should be the normal Hash instance. An abnormal means that Hash instance is the Hash of multiple Array instances along with with their keys or you can say that it the collection of multiple keys and values which are itself an object of Array class. Let us go through the syntax and demonstrating the program codes of this method.

此方法是Public實例方法,屬于Hash類,它位于Ruby語言庫中。 Hash.rassoc()方法用于檢查值(鍵值)是否為特定Hash實例的一部分,并且該Hash實例應為普通Hash實例。 異常意味著Hash實例是多個Array實例及其鍵的哈希,或者可以說它是多個鍵和值的集合,而這些鍵和值本身就是Array類的對象。 讓我們來看一下語法,并演示該方法的程序代碼。

If you are thinking about what it will return then let me tell you, it will return the first contained Hash instance where it found the presence of the value in Key-value pair. It will return "nil" if it hadn't found the value in any of the Hashes.

如果您正在考慮它將返回什么,那么讓我告訴您,它將返回第一個包含的Hash實例,在該實例中發現鍵值對中存在該值。 如果在任何哈希中都找不到該值,它將返回“ nil”

Syntax:

句法:

    Hash_instance.rassoc(obj)

Argument(s) required:

所需參數:

This method only takes one parameter and that argument is nothing but an object whose presence we want to check.

此方法僅使用一個參數,而該參數不過是一個要檢查其存在性的對象。

Example 1:

范例1:

=begin
Ruby program to demonstrate Hash.rassoc method
=end	
hsh = {"colors"  => "red",
"letters" => "a", "Fruit" => "Grapes"}
puts "Hash.rassoc implementation:"
puts "Enter the Value you want to search:-"
ky = gets.chomp
if (hsh.rassoc(ky))
puts "Value found successfully"
puts "Values are: #{hsh.rassoc(ky)}"
else
puts "Value not found!"
end

Output

輸出量

Hash.rassoc implementation:
Enter the Value you want to search:-
blue
Value not found!

Explanation:

說明:

In the above code, you can find that the Hash instance on which we have invoked the Hash.rassoc() method is a normal Hash instance. It is not the collection of multiple Array instances along with their specific keys. It is returning the whole Array instance with the key where it has found the value inputted by the user.

在上面的代碼中,您可以發現我們在其上調用Hash.rassoc()方法的Hash實例是普通的Hash實例。 它不是多個Array實例及其特定鍵的集合。 它返回整個Array實例,并返回找到用戶輸入值的鍵。

Example 2:

范例2:

=begin
Ruby program to demonstrate Hash.rassoc method
=end	
hsh = {"color"=> ["green","blue","yellow"],"vege"=> ["papaya","brinjal"]}
puts "Hash rassoc implementation:"
puts hsh.rassoc("green")

Output

輸出量

Hash rassoc implementation:

Explanation:

說明:

In the above code, you can verify that the Hash.rassoc() method does not work upon abnormal Hash instances. It will return nil even if the value is present in the Hash.

在上面的代碼中,您可以驗證Hash.rassoc()方法不適用于異常的Hash實例。 即使哈希中存在該值,它也將返回nil。

翻譯自: https://www.includehelp.com/ruby/hash-rassoc-obj-method-with-example.aspx

ruby hash方法

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

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

相關文章

JavaMail(四):接收郵件

2019獨角獸企業重金招聘Python工程師標準>>> 接收郵件采用POP3協議進行接收,簡單的一個接收郵件案例如下 /*** 接收郵件*/ public static void receive() throws Exception { // 準備連接服務器的會話信息 Properties props new Properties(); props.se…

html流動海報css,海報網css代碼怎樣把模塊移到右面?最好把代碼直接給我!嘿嘿。...

匿名用戶13級2010-09-15 回答CSS是Cascading style Sheets的簡稱,中文譯作“層疊樣式表單”. 實際上它是一組樣式。你可能對CSS這個名詞比較陌生,實際上無論你用Internet Explorer還是Netscape Navigator在網上沖浪,幾乎隨時都在與CSS打交道&…

奧鵬東北大學作業答案計算機網絡,東北大學17秋學期《計算機網絡》在線作業1參考答案...

一、單選題:【15道,總分:75分】東北大學1.IP地址0.0.0.0表示( )。 (滿分:5)A. 廣播B. 有限廣播C. 自環地址D. 本機2.IP協議中,如果首部不含選項字段,則首部長度字段的值應為( )。 (滿分:5)A. 0B. 5C. 10D. 203.滑動窗口的作用是…

append生成新變量的時候,沒有如預期(It's a feature,not a bug?)

這是我在寫一個項目中,遇到的一個golang的feature,如代碼所示,我在for循環里,每次用append生成一個新的數組,(當然我以前一直以為可以這樣,直到我在stackoverflow上發現不能。)然后將…

jqgrid本地數據例子_辦公系統私有云公有云和本地化部署,你選哪個?

隨著互聯網和云計算技術的發展,我們常常會聽到:私有云、公有云和本地化部署,它們分別代表什么意思?無論是國企、民企還是外企,只要有數據,就要思考:到底將數據儲存在哪里?選擇輕流進…

計算機基礎應用的培養活動記錄,小學少年宮計算機興趣小組活動記錄表

小學少年宮計算機興趣小組活動記錄表 小學少年宮計算機興趣小組活動記錄表 活動名稱:計算機 指導教師: ⅩⅩ 學生姓名 活動地點 計算機教室 活 動 過 程 活動1.認識計算機 ①師:“請同學們按下方盒子上面像大鈕扣一樣的按鈕&#…

C ++ STL中的set :: lower_bound()函數

C STL set :: lower_bound()函數 (C STL set::lower_bound() function) set::lower_bound() function is a predefined function, it is used to get the lower bound of any element in a set. set :: lower_bound()函數是預定義的函數,用于獲取集合中任何元素的下…

flume入門

# example.conf: A single-node Flume configuration# Name the components on this agent #給那三個組件取個名字 a1.sources r1 a1.sinks k1 a1.channels c1# Describe/configure the source #類型, 從網絡端口接收數據,在本機啟動, 所以localhost, typespoolDir采集目錄源…

原生js設置div隱藏或者顯示_JavaScript動畫方式控制div元素的隱藏和顯示

jQuery實現此功能相對比較簡單,具體參閱點擊按鈕動畫方式隱藏和顯示div一章節。結合CSS3實現此功能也非常便利和適合,具體參閱JavaScript與CSS3動畫方式改變元素尺寸一章節。原生JavaScript實現稍顯麻煩,下面就通過代碼實例介紹一下如何實現此…

python線程任務run_Python線程類| 帶有示例的run()方法

python線程任務runPython Thread.run()方法 (Python Thread.run() Method) Thread.run() method is an inbuilt method of the Thread class of the threading module in Python. This method is used to represent a threads activity. It calls the method expressed as the …

哪個計算機無法做到雙屏顯示,怎么啟用雙屏顯示設置【圖文介紹】

隨著生活質量的提升,人們對于便利的要求越來越高,電腦的雙屏顯示成為現在用戶越來越關注的對象之一。比如炒股操作中,人們既要關注大盤的走動趨勢,又要關注特定股票的走勢,在普通的屏幕上就需要不斷地切換頁面&#xf…

1.django 開發環境搭建

2019獨角獸企業重金招聘Python工程師標準>>> 本教程使用的開發環境 本教程寫作時開發環境的系統平臺為 Windows 10 (64 位),Python 版本為 3.5.2 (64 位),Django 版本為 1.10.6。 建議盡可能地與…

改jpg_|我來改第04期|—人物海報設計

【我來改第04期】人物海報文案內容-Ad copyhttps://pan.baidu.com/s/16pz9_rcLXTGZ3xzKFjKN9g 密碼: r57t設計要求-Requirement尺寸:1080(高)*650(寬)像素,大小不大于5m設計要求:以目前的藍色為主要顏色(R:47,G:87,B:130),畫面體…

Java即時類| plus()方法與示例

即時類plus()方法 (Instant Class plus() method) Syntax: 句法: public Instant plus(TemporalAmount t_amt);public Instant plus(long amt, TemporalUnit t_unit);plus() method is available in java.time package. plus()方法在java.time包中可用。 plus(Temp…

計算機領域中dns是什么意思,dns錯誤是什么意思?dns錯誤要如何解決?

隨著網絡的普及,電腦已經成為我們生活中必不可少的物品,它可以幫助我們工作和學習以及娛樂。不過電腦在使用的過程中,常常會出現一些問題,小編覺得最常見也最讓人心生煩躁的就是輸入網址打開之后出現網頁打不開,提示dn…

Launch Instruments

Launch Instruments 該儀器應用在Xcode應用程序,所以推出是在Xcode的最直接的方式。你也可以啟動它間接地通過碼頭,啟動,或命令行。The Instruments app lives inside the Xcode app, so the most direct way to launch it is from within Xc…

java clock計時_Java Clock類| offset()方法與示例

java clock計時Clock Class offset()方法 (Clock Class offset() method) offset() method is available in java.time package. offset()方法在java.time包中可用。 offset() method is used to generate a new Clock from the given base clock with added the given Duratio…

dataframe數據標準化處理_數據處理中的標準化、歸一化究竟是什么?

關注上方“Python數據科學”,選擇星標,精彩文章不會錯過!今天說一個比較重要的內容,無論是在算法建模還是在數據分析都比較常見:數據歸一化和標準化。開始之前,請你先把網上看到的所有相關的博客、帖子都忘…

北航計算機學院博士機試考試題,北航2015考博真題回憶及全套資料 - 考博 - 小木蟲 - 學術 科研 互動社區...

我是2015年畢業的應屆碩士,報考了北航經管院的博士。想寫這篇文很久了,現在復試完等最后結果中,把自己一路走來的經驗分享給大家。1.報名報名之前很重要的一點就是聯系導師。我是研二下,快放暑假了才開始聯系導師。北航一般是11月…

用了Redis里面的map和set

map的操作用 hset,hget等 set的操作有 sadd sismember等 參考下面: http://blog.csdn.net/kwsy2008/article/details/48467441