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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2025/3/11 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ruby hash方法_Ruby中带有示例的Hash.rassoc(obj)方法 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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)方法 。 由于該方法的名稱(chēng)完全不同,因此無(wú)法進(jìn)行假設(shè)。 讓我們閱讀其定義并在語(yǔ)法和程序代碼的幫助下了解其實(shí)現(xiàn)。

Method description:

方法說(shuō)明:

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實(shí)例方法,屬于Hash類(lèi),它位于Ruby語(yǔ)言庫(kù)中。 Hash.rassoc()方法用于檢查值(鍵值)是否為特定Hash實(shí)例的一部分,并且該Hash實(shí)例應(yīng)為普通Hash實(shí)例。 異常意味著Hash實(shí)例是多個(gè)Array實(shí)例及其鍵的哈希,或者可以說(shuō)它是多個(gè)鍵和值的集合,而這些鍵和值本身就是Array類(lèi)的對(duì)象。 讓我們來(lái)看一下語(yǔ)法,并演示該方法的程序代碼。

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.

如果您正在考慮它將返回什么,那么讓我告訴您,它將返回第一個(gè)包含的Hash實(shí)例,在該實(shí)例中發(fā)現(xiàn)鍵值對(duì)中存在該值。 如果在任何哈希中都找不到該值,它將返回“ nil”

Syntax:

句法:

Hash_instance.rassoc(obj)

Argument(s) required:

所需參數(shù):

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

此方法僅使用一個(gè)參數(shù),而該參數(shù)不過(guò)是一個(gè)要檢查其存在性的對(duì)象。

Example 1:

范例1:

=beginRuby 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.chompif (hsh.rassoc(ky))puts "Value found successfully"puts "Values are: #{hsh.rassoc(ky)}" elseputs "Value not found!" end

Output

輸出量

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

Explanation:

說(shuō)明:

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.

在上面的代碼中,您可以發(fā)現(xiàn)我們?cè)谄渖险{(diào)用Hash.rassoc()方法的Hash實(shí)例是普通的Hash實(shí)例。 它不是多個(gè)Array實(shí)例及其特定鍵的集合。 它返回整個(gè)Array實(shí)例,并返回找到用戶(hù)輸入值的鍵。

Example 2:

范例2:

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

說(shuō)明:

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.

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

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

ruby hash方法

總結(jié)

以上是生活随笔為你收集整理的ruby hash方法_Ruby中带有示例的Hash.rassoc(obj)方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。