ruby hash方法_Ruby中带有示例的Hash.values方法

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方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值