ruby hash方法_Hash.fetch()方法以及Ruby中的示例

ruby hash方法

Hash.fetch()方法 (Hash.fetch() Method)

In this article, we will study about Hash.fetch() Method. The working of this method can be predicted with the help of its name but it is not as simple as it seems. Well, we will understand this method with the help of its syntax and program code in the rest of the content.

在本文中,我们将研究Hash.fetch()方法 。 可以借助其名称来预测此方法的工作,但是它并不像看起来那样简单。 好了,我们将在其余内容中借助其语法和程序代码来理解此方法。

Method description:

方法说明:

This method is a public instance method that is defined in the ruby library especially for Hash class. This method requires keys whose values are fetched by this method. This method works in the way that it returns the value or values from the hash object for a given key or keys. In case the key is not found in the hash instance then the result is dependent upon the type of method call done. The possible method calls are listed below:

此方法是在ruby库中定义的公共实例方法,特别是针对Hash类。 此方法需要通过此方法获取其值的键。 此方法的工作方式是,它从给定键或多个键的哈希对象中返回一个或多个值。 如果在哈希实例中找不到键,则结果取决于完成的方法调用的类型。 下面列出了可能的方法调用:

  • Without any other argument: If no other argument is provided and the key is not found then the method will throw a "KeyError" exception.

    没有任何其他自变量 :如果未提供其他自变量且未找到键,则该方法将引发“ KeyError”异常。

  • With other arguments (s): The other argument is considered as the default argument. If the key is not found then that default argument will be returned from the method.

    与其他参数一起使用 :另一个参数被视为默认参数。 如果找不到该键,则将从该方法返回该默认参数。

  • With block: If the block is provided and the key is not found then that block will be run and the result will be returned in case the key is not found.

    With块 :如果提供了该块,但未找到键,则该块将运行,如果未找到键,则将返回结果。

Syntax:

句法:

    Hash_object.fetch(…,[default]);
    or
    Hash_object_fetch(key(s))
    or
    Hash_object_fetch{|key| block}

Argument(s) required:

所需参数:

There is no restriction upon passing the arguments. You can pass arguments as per your requirement. The last argument will always be considered as the default argument.

传递参数没有限制。 您可以根据需要传递参数。 最后一个参数将始终被视为默认参数。

Example 1:

范例1:

=begin
  Ruby program to demonstrate fetch method
=end	

hash1={"color"=>"Black","object"=>"phone","love"=>"mom","fruit"=>"Kiwi","vege"=>"potato"}

puts "Hash.fetch implementation"

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

if(hash1.fetch(ky))
	puts "Key found successfully. The value is #{hash1.fetch(ky)}"
else
	puts "No key found"
end

Output

输出量

RUN 1:
Hash.fetch implementation
Enter the key you want to search:
 color
Key found successfully. The value is Black

RUN 2:
Hash.fetch implementation
Enter the key you want to search:
 velvet
key not found: "velvet"
(repl):12:in `fetch'
(repl):12:in `<main>'

Explanation:

说明:

In the above code, you can simply observe that we have invoked the method with a key which is being asked by the user. In Run 2, you may see that the exception is thrown when the key is not found in the hash.

在上面的代码中,您可以简单地观察到我们已经使用了用户要求的密钥来调用该方法。 在运行2中,您可能会看到在哈希中找不到键时引发了异常。

Example 2:

范例2:

=begin
  Ruby program to demonstrate fetch method
=end	

hash1={"color"=>"Black","object"=>"phone","love"=>"mom","fruit"=>"Kiwi","vege"=>"potato"}

puts "Hash.fetch implementation"

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

puts "The value of #{ky} is #{hash1.fetch(ky,"Not found")}"

Output

输出量

Hash.fetch implementation
Enter the key you want to search:
 cloth
The value of cloth is Not found

Explanation:

说明:

In the above code, you can observe that we have passed a default argument along with the key inside the method. You can see that the default object is returned when the key is not found in the hash instance.

在上面的代码中,您可以观察到我们已经在方法内部传递了默认参数以及键。 您可以看到,在哈希实例中未找到键时,将返回默认对象。

Example 3:

范例3:

=begin
  Ruby program to demonstrate fetch method
=end	

hash1={"color"=>"Black","object"=>"phone","love"=>"mom","fruit"=>"Kiwi","vege"=>"potato"}

puts "Hash.fetch implementation"

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

puts "The value of #{ky} is #{hash1.fetch(ky){|ky| "Sorry! #{ky} is missing"}}"

Output

输出量

Hash.fetch implementation
Enter the key you want to search:
 cloth
The value of cloth is Sorry! cloth is missing

Explanation:

说明:

In the above code, you can observe that we are passing a block along with the method. That block has been run and its value has been returned when the key is not found in the hash object.

在上面的代码中,您可以观察到我们将块与方法一起传递。 当在哈希对象中找不到键时,该块已经运行,并且其值已返回。

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

ruby hash方法

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值