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

ruby hash方法

哈希键方法 (Hash.keys Method)

In this article, we will study about Hash.keys Method. The working of the method can't be assumed because of it's quite a different name. Let us read its definition and understand its implementation with the help of syntax and program codes.

在本文中,我们将研究Hash.keys方法 。 由于该方法的名称完全不同,因此无法进行假设。 让我们阅读其定义并在语法和程序代码的帮助下了解其实现。

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 contacting all the keys which are present in the hash object. In simpler language, you can say that the new array will be containing the keys from the self hash object. It will return an empty array if no values are present in the hash objects.

此方法是Public实例方法,属于Hash类,它位于Ruby语言库中。 此方法的工作方式是返回一个数组,该数组联系哈希对象中存在的所有键。 用更简单的语言,您可以说新数组将包含来自自我哈希对象的键。 如果哈希对象中没有值,它将返回一个空数组。

Syntax:

句法:

    Hash.keys

Example 1:

范例1:

=begin
  Ruby program to demonstrate Hash.keys method
=end	

hsh = {"colors"  => "red","letters" => "a", "Fruit" => "Grapes", "anything"=>"red","sweet"=>"ladoo"}

puts "Hash.keys implementation:"
ary = hsh.keys

puts "The keys present in the hash are: #{ary}"

puts "Self hash object: #{hsh}"

Output

输出量

Hash.keys implementation:
The keys present in the hash are: ["colors", "letters", "Fruit", "anything", "sweet"]
Self hash object: {"colors"=>"red", "letters"=>"a", "Fruit"=>"Grapes", "anything"=>"red", "sweet"=>"ladoo"}

Explanation:

说明:

In the above code, you can observe that you can find all the keys present in the hash with the help of the Hash.keys method. The array instance returned from the hash will be populated with all the keys present in the specific hash object. This is very clear with the help of the output drawn from the program code given for the demonstration purpose.

在上面的代码中,您可以观察到借助Hash.keys方法可以找到哈希中存在的所有键。 从哈希返回的数组实例将使用特定哈希对象中存在的所有键填充。 借助于为演示目的给出的程序代码得出的输出,这一点非常清楚。

Example 2:

范例2:

=begin
  Ruby program to demonstrate Hash.keys method
=end	

hsh = Hash.new()

puts "Hash.keys implementation:"

ary = hsh.keys

puts "The keys present in the hash are: #{ary}"

puts "Self hash object: #{hsh}"

Output

输出量

Hash.keys implementation:
The keys present in the hash are: []
Self hash object: {}

Explanation:

说明:

In the above code, you can observe that we are trying to fetch all the keys from the hash with the help of the Hash.keys method. This method is returning an empty array if the hash is empty or you can say that it doesn't have any key-value pair. This is very clear with the help of the output drawn by the program code.

在上面的代码中,您可以观察到我们正在尝试借助Hash.keys方法从哈希中获取所有键。 如果哈希为空,或者您可以说它没有任何键值对,则此方法将返回一个空数组。 借助于程序代码绘制的输出,这非常清楚。

翻译自: https://www.includehelp.com/ruby/hash-keys-method-with-example.aspx

ruby hash方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值