ruby 生成哈希值_如何检查Ruby中是否存在哈希键?
ruby 生成哈希值
We know that Hashes are the instances which are the combination of keys and their values. A particular key in hash may contain more than one value as well. In this article, we will see how we can check or test the presence of a particular key in hash object? There are multiple ways to tackle this problem.
我們知道哈希是鍵及其值組合的實例。 哈希中的特定鍵也可能包含多個值。 在本文中,我們將看到如何檢查或測試哈希對象中特定鍵的存在? 有多種方法可以解決此問題。
Let us discuss a few of them,
讓我們討論其中的一些,
Method 1: With the help of has_key?() method
方法1:借助has_key?()方法
This method is a Public instance method and belongs to the Hash class which lives inside the library of Ruby language. Hash.has_key?() method is used to check whether a key(key-value) is a part of the particular Hash instance or not and that Hash instance should be a normal Hash instance. It will search through the whole Hash and gives you the result according to its search. Let us go through the syntax and demonstrating the program codes of this method.
此方法是Public實例方法,屬于Hash類,它位于Ruby語言庫中。 Hash.has_key?()方法用于檢查鍵(鍵值)是否為特定Hash實例的一部分,并且該Hash實例應為普通Hash實例。 它將搜索整個哈希,并根據其搜索結果。 讓我們來看一下語法,并演示該方法的程序代碼。
If you are thinking about what it will return then let me tell you, it will return a Boolean value. The returned value will be true if it finds the key inside the Hash and the return value will be false if it does not find the key to be the part of Hash instance.
如果您正在考慮它將返回什么,那么讓我告訴您,它將返回一個布爾值。 如果在哈希表中找到密鑰,則返回值將為true;如果找不到哈希表實例的一部分,則返回值為false。
Syntax:
句法:
Hash_instance.has_key?(obj)Parameter(s) required:
所需參數:
This method only takes one parameter and that argument is nothing but the key whose presence we want to check.
此方法僅使用一個參數,而該參數不過是我們要檢查其存在性的鍵。
Program:
程序:
=beginRuby program to demonstrate Hash.has_key() method =end hsh = {"colors" => "red","letters" => "a", "Fruit" => "Grapes"}puts "Hash.has_key implementation:" puts "Enter the Key you want to search:-" ky = gets.chompif (hsh.has_key?(ky))puts "Key found successfully" elseputs "Key not found!" endOutput
輸出量
Hash.has_key implementation: Enter the Key you want to search:-colors Key found successfullyExplanation:
說明:
In the above code, you can observe that we are invoking the Hash.has_key?() method on the normal Hash instance. It has returned true when it found the presence of the key in the Hash object which is entered by the user.
在上面的代碼中,您可以觀察到我們在普通的Hash實例上調用Hash.has_key?()方法。 當它在用戶輸入的哈希對象中發現密鑰存在時,它返回true。
Method 2: With the help of assoc() method
方法2:借助于assoc()方法
The above method will only tell you the presence of key but assoc() method will return the set of values associated with that particular key.
上面的方法只會告訴您鍵的存在,而assoc()方法將返回與該特定鍵關聯的一組值。
This method is a public instance method and belongs to the Hash class which lives inside the library of Ruby language. This method is used to check whether an object(key-value) is a part of the particular Hash instance or not and that Hash instance can or cannot be the normal Hash instance. If it is not normal, it means that Hash instance is the Hash of multiple Array instances along 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.
該方法是一個公共實例方法,屬于Hash類,該類存在于Ruby語言庫中。 此方法用于檢查對象(鍵值)是否是特定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 Key-value pair. It will return 'nil' if it hadn't found the object in any of the Hashes.
如果您正在考慮它將返回什么,那么讓我告訴您,它將返回第一個包含的Hash實例,在該實例中找到鍵值對。 如果未在任何哈希中找到對象,它將返回“ nil ”。
Syntax:
句法:
Hash_instance.assoc(obj)Parameter(s) required:
所需參數:
This method only takes one parameter and that argument is nothing but an object whose presence we want to check.
此方法僅使用一個參數,而該參數不過是一個要檢查其存在性的對象。
Program:
程序:
=beginRuby program to demonstrate Hash.assoc() method =end hsh = {"colors" => ["red", "blue", "green"],"letters" => ["a", "b", "c" ], "Fruit" => ["Banana","Kiwi","Grapes"]}puts "Hash.assoc implementation:" puts "Enter the Key you want to search:-" ky = gets.chompif (hsh.assoc(ky))puts "Key found successfully"puts "Values are: #{hsh.assoc(ky)}" elseputs "Key not found!" endOutput
輸出量
Hash.assoc implementation: Enter the Key you want to search:-colors Key found successfully Values are: ["colors", ["red", "blue", "green"]]Method 3 : With the help of member?() method
方法3:借助member?()方法
This method is a public instance method and belongs to the Hash class which lives inside the library of Ruby language. Hash.member?() method is used to check whether a key(key-value) is a part of the particular Hash instance or not and that Hash instance should be the normal Hash instance. It will search through the whole Hash and gives you the result according to its search. Let us go through the syntax and demonstrating the program codes of this method.
該方法是一個公共實例方法,屬于Hash類,該類存在于Ruby語言庫中。 Hash.member?()方法用于檢查鍵(鍵值)是否為特定Hash實例的一部分,并且該Hash實例應為普通Hash實例。 它將搜索整個哈希,并根據其搜索結果。 讓我們來看一下語法,并演示該方法的程序代碼。
If you are thinking about what it will return then let me tell you, it will return a Boolean value. The returned value will be true if it finds the key inside the Hash and the return value will be false if it does not find the key to be the part of Hash instance.
如果您正在考慮它將返回什么,那么讓我告訴您,它將返回一個布爾值。 如果在哈希表中找到密鑰,則返回值將為true;如果找不到哈希表實例的一部分,則返回值為false。
Syntax:
句法:
Hash_instance.member?(obj)Parameter(s) required:
所需參數:
This method only takes one parameter and that argument is nothing but the key whose presence we want to check.
此方法僅使用一個參數,而該參數不過是我們要檢查其存在性的鍵。
Program:
程序:
=beginRuby program to demonstrate Hash.member method =end hsh = {"colors" => "red","letters" => "a", "Fruit" => "Grapes"}puts "Hash.member? implementation:" puts "Enter the Key you want to search:-" ky = gets.chompif (hsh.member?(ky))puts "Key found successfully" elseputs "Key not found!" endOutput
輸出量
Hash.member? implementation: Enter the Key you want to search:-colors Key found successfullyExplanation:
說明:
In the above code, you can observe that we are invoking the Hash.member?() method on the normal Hash instance. It has returned true when it found the presence of the key in the Hash object which is entered by the user.
在上面的代碼中,您可以觀察到我們在普通的Hash實例上調用Hash.member?()方法。 當它在用戶輸入的哈希對象中發現密鑰存在時,它返回true。
翻譯自: https://www.includehelp.com/ruby/how-to-check-if-a-hash-key-exists-in-ruby.aspx
ruby 生成哈希值
總結
以上是生活随笔為你收集整理的ruby 生成哈希值_如何检查Ruby中是否存在哈希键?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: go newscanner判断文件读取结
- 下一篇: SSIA的完整形式是什么?