Ruby中带有示例的Hash.key?(value)方法

Hash.key?(value)方法 (Hash.key?(value) Method)

In this article, we will study about Hash.key?(value) 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.key?(value)方法 。 无法假定该方法的工作原理,因为它的名称完全不同。 让我们阅读其定义并在语法和程序代码的帮助下了解其实现。

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 checks the presence of a particular value in the hash object and returns it's key if the value is present in the hash. This value will return nil if it doesn't find the presence of value which is asked by the user which is passed with the method at the time of its invocation.

此方法是Public实例方法,属于Hash类,它位于Ruby语言库中。 此方法的工作方式是检查哈希对象中是否存在特定值,如果哈希中存在该值,则返回其键。 如果找不到调用该方法时传递的用户要求的值,则此值将返回nil。

Syntax:

句法:

    Hash.key(value)

Argument(s) required:

所需参数:

This method only requires one argument and that value is nothing but the value of the key you want to get.

此方法仅需要一个参数,并且该值仅是您要获取的键的值。

Example 1:

范例1:

=begin
  Ruby program to demonstrate Hash.key(value) method
=end	

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

puts "Hash.key(value) implementation:"

puts "Enter the value you want to search:"
value = gets.chomp

if (hsh.key(value))
	puts "Value found successfully key is #{hsh.key(value)}"
else
	puts "Key not found!"
end

Output

输出量

Hash.key(value) implementation:
Enter the value you want to search:
 red
Value found successfully key is colors

Explanation:

说明:

In the above code, you can observe that we are finding keys with the help of values by using Hash.key(value) method. You can see that we are asking the user for value whose key he/she wants to find. First, we are checking whether the value is present in the hash or not. If it is not present in the hash object then the method will return 'nil' and you will not get anything as the result.

在上面的代码中,您可以观察到我们正在使用Hash.key(value)方法在值的帮助下找到键。 可以看到,我们在向用户询问他/她想找到其键的值。 首先,我们正在检查该值是否存在于哈希中。 如果哈希对象中不存在该对象,则该方法将返回“ nil”,并且您将不会得到任何结果。

Example 2:

范例2:

=begin
  Ruby program to demonstrate Hash.key(value) method
=end	

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

puts "Hash.key(value) implementation:"

puts "Enter the value you want to search:"
value = gets.chomp

if (hsh.key(value))
	puts "Value found successfully key is #{hsh.key(value)}"
else
	puts "Key not found!"
end

Output

输出量

Hash.key(value) implementation:
Enter the value you want to search:
 red
Value found successfully key is colors

Explanation:

说明:

With the help of the above code, it is demonstrated that when there are two keys present in the hash object with the same value then instead of returning both the keys, this method returns only first key in which it has found the presence of value.

借助于以上代码,证明了当哈希对象中存在两个具有相同值的键时,而不是返回两个键,此方法仅返回发现值存在的第一个键。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值