as_hash ruby_Ruby中带有示例的Hash.has_key?()方法

as_hash ruby

Hash.has_key?()方法 (Hash.has_key?() Method)

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

Method description:

方法说明:

This method is a Public instance method and belongs to the Hash class which lives inside the library of Ruby language. Hash.has_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.has_key?()方法用于检查key( key-value )是否是特定Hash实例的一部分,并且该Hash实例应该是普通的Hash实例。 它将搜索整个哈希,并根据其搜索结果。 让我们来看一下语法,并演示该方法的程序代码。

If you are thinking, 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.has_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.has_key method
=end	

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

puts "Hash.has_key implementation:"

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

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

Output

输出量

Hash.has_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.has_key() method on the normal Hash instance. It has returned true when it found the presence of the key in the Hash object which is entered by the user.

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

Example 2:

范例2:

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

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

hsh1 = {"cars"  => "800",
     "bike" => "pulsar", "phone" => "A50"}

hsh2 = {"one"=> hsh, "two" => hsh1}

puts "Hash.has_key implementation:"

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

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

Output

输出量

Hash.has_key implementation:
Enter the Key you want to search:
 cars
Key not found!

Explanation:

说明:

In the above, you can verify that Hash.has_key?() the 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.has_key?()方法是否不适用于Hash实例,该实例是多​​个Hash实例的集合。 即使对象是Hash实例的一部分,它也会返回false。

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

as_hash ruby

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值