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:
程序:
=begin
Ruby 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.chomp
if (hsh.has_key?(ky))
puts "Key found successfully"
else
puts "Key not found!"
end
Output
输出量
Hash.has_key implementation:
Enter the Key you want to search:-
colors
Key found successfully
Explanation:
说明:
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:
程序:
=begin
Ruby 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.chomp
if (hsh.assoc(ky))
puts "Key found successfully"
puts "Values are: #{hsh.assoc(ky)}"
else
puts "Key not found!"
end
Output
输出量
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:
程序:
=begin
Ruby 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.chomp
if (hsh.member?(ky))
puts "Key found successfully"
else
puts "Key not found!"
end
Output
输出量
Hash.member? implementation:
Enter the Key you want to search:-
colors
Key found successfully
Explanation:
说明:
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 生成哈希值