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!" endOutput
輸出量
Hash.key(value) implementation: Enter the value you want to search:red Value found successfully key is colorsExplanation:
說明:
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!" endOutput
輸出量
Hash.key(value) implementation: Enter the value you want to search:red Value found successfully key is colorsExplanation:
說明:
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)方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 凯撒密码pythin密码_凯撒密码术
- 下一篇: 算法导论 算法_算法导论