ruby hash方法
哈希值方法 (Hash.values Method)
In this article, we will study about Hash.values Method. The working of the method can be assumed because of its very common name but there exist some hidden complexities too. Let us read its definition and understand its implementation with the help of syntax and program codes.
在本文中,我们将研究Hash.values方法 。 可以假定该方法的工作是由于其非常通用的名称,但也存在一些隐藏的复杂性。 让我们阅读其定义并在语法和程序代码的帮助下了解其实现。
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 returns an array that contains all the values present in the hash object. This method traverses the whole hash object and stores the values inside the array to be returned with a separate index. The Hash.values() method will return an empty array instance if there is no value present inside the hash object.
此方法是Public实例方法,属于Hash类,它位于Ruby语言库中。 此方法的工作方式是返回一个包含哈希对象中存在的所有值的数组。 此方法遍历整个哈希对象,并将值存储在要返回的数组中并带有单独的索引。 如果哈希对象内部不存在任何值,则Hash.values()方法将返回一个空数组实例。
Syntax:
句法:
Hash_object.values
Argument(s) required:
所需参数:
This method does not take any parameter. It returns a hash which contains all the values of hash object.
此方法不带任何参数。 它返回一个包含哈希对象所有值的哈希。
Example 1:
范例1:
=begin
Ruby program to demonstrate Hash.values method
=end
hsh = {"colors" => "red","city"=>"Nainital", "Fruit" => "Grapes", "anything"=>"red","sweet"=>"ladoo"}
puts "Hash.values implementation"
ary = hsh.values
puts "Values present in the hash are:"
ary.each do |val|
puts val
end
Output
输出量
Hash.values implementation
Values present in the hash are:
red
Nainital
Grapes
red
ladoo
Explanation:
说明:
In the above code, you can observe that we can get all the values present inside a particular hash object with the help of the Hash.values() method. You can see that all the values are stored inside an array object which can be displayed.
在上面的代码中,您可以观察到,借助于Hash.values()方法 ,我们可以获取特定哈希对象内存在的所有值。 您会看到所有值都存储在可以显示的数组对象中。
Example 2:
范例2:
=begin
Ruby program to demonstrate Hash.values method
=end
hsh = {"colors" => ["red","blue","green"],"city"=>"Nainital", "Fruit" => "Grapes", "anything"=>"red","sweet"=>"ladoo"}
puts "Hash.values implementation"
ary = hsh.values
puts "Values present in the hash are:"
puts "#{ary}"
Output
输出量
Hash.values implementation
Values present in the hash are:
[["red", "blue", "green"], "Nainital", "Grapes", "red", "ladoo"]
Explanation:
说明:
In the above code, you can observe that you can fetch all the values from a hash object with the help of the Hash.values() method. In the above code, you can observe that a multidimensional array object has been returned because the hash object was containing an array of values stored inside a particular key.
在上面的代码中,您可以观察到可以借助Hash.values()方法从哈希对象中获取所有值。 在上面的代码中,您可以观察到已返回多维数组对象,因为哈希对象包含存储在特定键中的值的数组。
翻译自: https://www.includehelp.com/ruby/hash-values-method-with-example.aspx
ruby hash方法