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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Ruby中带有示例的Hash.key?(value)方法

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

Hash.key?(value)方法 (Hash.key?(value) Method)

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

在本文中,我們將研究Hash.key?(value)方法 。 無法假定該方法的工作原理,因為它的名稱完全不同。 讓我們閱讀其定義并在語法和程序代碼的幫助下了解其實現。

Method description:

方法說明:

This method is a Public instance method and belongs to the Hash class which lives inside the library of Ruby language. This method works in a way that it checks the presence of a particular value in the hash object and returns it's key if the value is present in the hash. This value will return nil if it doesn't find the presence of value which is asked by the user which is passed with the method at the time of its invocation.

此方法是Public實例方法,屬于Hash類,它位于Ruby語言庫中。 此方法的工作方式是檢查哈希對象中是否存在特定值,如果哈希中存在該值,則返回其鍵。 如果找不到調用該方法時傳遞的用戶要求的值,則此值將返回nil。

Syntax:

句法:

Hash.key(value)

Argument(s) required:

所需參數:

This method only requires one argument and that value is nothing but the value of the key you want to get.

此方法僅需要一個參數,并且該值僅是您要獲取的鍵的值。

Example 1:

范例1:

=beginRuby program to demonstrate Hash.key(value) method =end hsh = {"colors" => "red","letters" => "a", "Fruit" => "Grapes"}puts "Hash.key(value) implementation:"puts "Enter the value you want to search:" value = gets.chompif (hsh.key(value))puts "Value found successfully key is #{hsh.key(value)}" elseputs "Key not found!" end

Output

輸出量

Hash.key(value) implementation: Enter the value you want to search:red Value found successfully key is colors

Explanation:

說明:

In the above code, you can observe that we are finding keys with the help of values by using Hash.key(value) method. You can see that we are asking the user for value whose key he/she wants to find. First, we are checking whether the value is present in the hash or not. If it is not present in the hash object then the method will return 'nil' and you will not get anything as the result.

在上面的代碼中,您可以觀察到我們正在使用Hash.key(value)方法在值的幫助下找到鍵。 可以看到,我們在向用戶詢問他/她想找到其鍵的值。 首先,我們正在檢查該值是否存在于哈希中。 如果哈希對象中不存在該對象,則該方法將返回“ nil”,并且您將不會得到任何結果。

Example 2:

范例2:

=beginRuby program to demonstrate Hash.key(value) method =end hsh = {"colors" => "red","letters" => "a", "Fruit" => "Grapes", "anything"=>"red"}puts "Hash.key(value) implementation:"puts "Enter the value you want to search:" value = gets.chompif (hsh.key(value))puts "Value found successfully key is #{hsh.key(value)}" elseputs "Key not found!" end

Output

輸出量

Hash.key(value) implementation: Enter the value you want to search:red Value found successfully key is colors

Explanation:

說明:

With the help of the above code, it is demonstrated that when there are two keys present in the hash object with the same value then instead of returning both the keys, this method returns only first key in which it has found the presence of value.

借助于以上代碼,證明了當哈希對象中存在兩個具有相同值的鍵時,而不是返回兩個鍵,此方法僅返回發現值存在的第一個鍵。

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

總結

以上是生活随笔為你收集整理的Ruby中带有示例的Hash.key?(value)方法的全部內容,希望文章能夠幫你解決所遇到的問題。

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