ruby hash方法
Hash.assoc()方法 (Hash.assoc() Method)
In this article, we will study about Hash.assoc() 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.assoc()方法 。 无法假定该方法的工作原理,因为它的名称完全不同。 让我们阅读其定义并在语法和程序代码的帮助下了解其实现。
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 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 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实例方法,属于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)
Argument(s) required:
所需参数:
This method only takes one parameter and that argument is nothing but an object whose presence we want to check.
此方法仅使用一个参数,而该参数不过是一个要检查其存在性的对象。
Example 1:
范例1:
=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
输出量
RUN 1:
Hash.assoc implementation:
Enter the Key you want to search:
colors
Key found successfully
Values are: ["colors", ["red", "blue", "green"]]
RUN 2:
Hash.assoc implementation:
Enter the Key you want to search:
veges
Key not found!
Explanation:
说明:
In the above code, you can find that the Hash instance on which we have invoked assoc() method is not any normal Hash instance. It is 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 key inputted by the user.
在上面的代码中,您可以发现我们调用assoc()方法的 Hash实例不是任何普通的Hash实例。 它是多个Array实例及其特定键的集合。 它在找到用户输入的键的情况下返回带有键的整个Array实例。
Example 2:
范例2:
=begin
Ruby program to demonstrate Hash.assoc method
=end
hsh = {"color"=> "green","vege"=> "papaya"}
puts "Hash assoc implementation:"
puts hsh.assoc("color")
Output
输出量
Hash assoc implementation:
color
green
Explanation:
说明:
In the above you can verify that assoc() method also works upon normal Hash instances. It will return the key-value pairs if the key is a part of the Hash instance.
在上面,您可以验证assoc()方法也可以在常规Hash实例上使用。 如果键是Hash实例的一部分,它将返回键值对。
翻译自: https://www.includehelp.com/ruby/hash-assoc-method-with-example.aspx
ruby hash方法