ruby hash方法_Ruby中带有示例的Hash.key?(obj)方法

ruby hash方法

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

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

Method description:

方法说明:

This method is a Public instance method and belongs to the Hash class which lives inside the library of Ruby language. Hash.key?() method is used to check whether a key(key-value) is a part of the particular Hash instance or not and that Hash instance should be a normal Hash instance. It will search through the whole Hash and gives you the result according to its search. Let us go through the syntax and demonstrating the program codes of this method.

此方法是Public实例方法,属于Hash类,它位于Ruby语言库中。 Hash.key?()方法用于检查键(键值)是否为特定Hash实例的一部分,并且该Hash实例应为普通Hash实例。 它将搜索整个哈希,并根据其搜索结果。 让我们来看一下语法,并演示该方法的程序代码。

If you are thinking about what it will return then let me tell you, it will return a Boolean value. The returned value will be true if it finds the key inside the Hash and the return value will be false if it does not find the key to be the part of Hash instance.

如果您正在考虑它将返回什么,那么让我告诉您, 它将返回一个布尔值 。 如果在哈希表中找到密钥,则返回值将为true;如果找不到哈希表实例的一部分,则返回值为false。

Syntax:

句法:

    Hash_instance.key?(obj)

Argument(s) required:

所需参数:

This method only takes one parameter and that argument is nothing but the key whose presence we want to check.

此方法仅使用一个参数,而该参数不过是我们要检查其存在性的键。

Example 1:

范例1:

=begin
  Ruby program to demonstrate Hash.key? method
=end	

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

puts "Hash.key? implementation:"

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

if (hsh.key?(ky))
	puts "Key found successfully"
else
	puts "Key not found!"
end

Output

输出量

Hash.key? implementation:
Enter the Key you want to search: 
 colors
Key found successfully

Explanation:

说明:

In the above code, you can observe that we are invoking the Hash.key?() method on the normal Hash instance. It has returned true when it found the presence of key in the Hash object which is entered by the user.

在上面的代码中,您可以观察到我们在普通的Hash实例上调用Hash.key?()方法 。 当它在用户输入的哈希对象中发现密钥存在时,它返回true。

Example 2:

范例2:

=begin
  Ruby program to demonstrate Hash.key? method
=end	

hsh = {"colors"  => "red","letters" => "a", "Fruit" => "Grapes"}
hsh1 = {"cars"  => "800","bike" => "pulsar", "phone" => "A50"}
hsh2 = {"one"=> hsh, "two" => hsh1}

puts "Hash.key? implementation:"

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

if (hsh2.key?(ky))
	puts "Key found successfully"
else
	puts "Key not found!"
end

Output

输出量

Hash.key? implementation:
Enter the Key you want to search: 
 colors
Key not found!

Explanation:

说明:

In the above code, you can verify that Hash.key?() method does not work upon Hash instance which is the collection of multiple Hash instances. It will return false even if the object is a part of the Hash instance.

在上面的代码中,您可以验证Hash.key?()方法不适用于Hash实例,该实例是多​​个Hash实例的集合。 即使对象是Hash实例的一部分,它也会返回false。

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

ruby hash方法

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值