as_hash ruby_Hash.fetch_values()方法与Ruby中的示例

as_hash ruby

Hash.fetch_values()方法 (Hash.fetch_values() Method)

In this article, we will study about Hash.fetch_values() 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_values()方法 。 可以借助其名称来预测此方法的工作,但是它并不像看起来那样简单。 好了,我们将在其余内容中借助其语法和程序代码来理解此方法。

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 array instance containing value or values from the hash object for a given key or keys. In case the key is not found in the hash object, an exception is thrown by the method and it is termed as "keyless" exception. For doing some kind of manipulation with the key values, you can even pass a block with the method.

此方法是在ruby库中定义的公共实例方法,特别是针对Hash类。 此方法需要通过此方法获取其值的键。 此方法的工作方式是,它返回一个数组实例,该实例包含一个或多个给定键的哈希对象中的一个或多个值。 如果在哈希对象中找不到键,则该方法会引发异常,该异常称为“无键”异常 。 为了对键值进行某种操作,您甚至可以使用方法传递一个块。

Syntax:

句法:

    Hash_object.fetch_values(key1, key2,…, keyn)
    or
    Hash_object_fetch_values(key1, key2,…, keyn){|key| block}

Argument(s) required:

所需参数:

There is no restriction upon passing the arguments. You can pass arguments as per your requirement.

传递参数没有限制。 您可以根据需要传递参数。

Example 1:

范例1:

=begin
  Ruby program to demonstrate fetch_values method
=end	

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

puts "Hash.fetch_values implementation"

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

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

if(hash1.fetch_values(ky1,ky2))
	puts "Key found successfully. The values ar #{hash1.fetch_values(ky1,ky2)}"
else
	puts "One of the keys are missing"
end

Output

输出量

RUN 1:
Hash.fetch_values implementation
Enter the first key you want to search:
 color
Enter the second key you want to search:
 object
Key found successfully. The values ar ["Black", "phone"]

RUN 2:
Hash.fetch_values implementation
Enter the first key you want to search:
 cloth
Enter the second key you want to search:
 color
key not found: "cloth"
(repl):15:in `fetch_values'
(repl):15:in `<main>'

Explanation:

说明:

In the above code, you may observe that this method returns an array of the values. These values are nothing but the values of the key passed along with the method. In Run 2, you can observe that even if the single key is missing the method will throw a "Keyless" exception

在上面的代码中,您可能会观察到该方法返回值的数组。 这些值不过是与方法一起传递的键的值。 在运行2中,您可以观察到即使缺少单个键,该方法也会引发“ Keyless”异常

Example 2:

范例2:

=begin
  Ruby program to demonstrate fetch_values method
=end	

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

puts "Hash.fetch_values implementation"

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

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

if(hash1.fetch_values(ky1,ky2){|ky| ky.upcase})
	puts "Key found successfully. The values are #{hash1.fetch_values(ky1,ky2){|ky| ky.upcase}}"
else
	puts "One of the keys are missing"
end

Output

输出量

Hash.fetch_values implementation
Enter the first key you want to search:
 color
Enter the second key you want to search:
 car
Key found successfully. The values are ["Black", "CAR"]

Explanation:

说明:

In the above code, you can observe that when we are passing a block then the key which is not found is converted into uppercase. This protects us from getting the exception.

在上面的代码中,您可以观察到,当我们传递一个块时,未找到的键将转换为大写。 这样可以保护我们免于例外。

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

as_hash ruby

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值