as_hash ruby_Hash.default_proc = obj方法(带Ruby中的示例)

as_hash ruby

Hash.default_proc = obj方法 (Hash.default_proc=obj Method)

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

Method description:

方法说明:

This method is a public instance method that is defined in the ruby library especially for Hash class. This method is used to set the default value and that value will be returned when the key is not found while key lookup. By this method, you can set procs as the default value for any hash object. If you don't set a default value then nil will be returned when the key is not found or key is not a part of the hash instance.

此方法是在ruby库中定义的公共实例方法,特别是针对Hash类。 此方法用于设置默认值,并且在查找密钥时未找到密钥时将返回该值。 通过此方法,您可以将procs设置为任何哈希对象的默认值 。 如果未设置默认值,则在找不到密钥或密钥不是哈希实例的一部分时将返回nil。

Syntax:

句法:

    Hash_object.default_proc

Argument(s) required:

所需参数:

This method does not require any argument. This method is used to assign value.

此方法不需要任何参数。 此方法用于分配值。

Example 1:

范例1:

=begin
  Ruby program to demonstrate default_proc method
=end	

hsh = Hash.new()

hsh["color"] = "Black"
hsh["age"] = 20
hsh["school"] = "Angels' Academy Haridwar"
hsh["college"] = "Graphic Era University"

puts "Hash default_proc implementation"

hsh.default_proc = proc do|hash,key|
	hsh[key]=key+key
end

puts "Hash contents are : #{hsh}"

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

puts "The value of #{ky} is #{hsh[ky]}"

Output

输出量

Hash default_proc implementation
Hash contents are : {"color"=>"Black", "age"=>20, "school"=>"Angels' Academy Haridwar", "college"=>"Graphic Era University"}
Enter the key you want to find:
 residence
The value of residence is residenceresidence

Explanation:

说明:

In the above code, you can observe that we are setting the proc as the default value of hash with the help of the default_proc method. You can see that when the user has entered a key that is not available in the hash, then the value returned from the proc has been printed.

在上面的代码中,您可以观察到,借助于default_proc方法 ,我们将proc设置为hash的默认值。 您可以看到,当用户输入了哈希中不可用的键时,从proc返回的值就已经打印了。

Example 2:

范例2:

=begin
  Ruby program to demonstrate setting proc with default method.
=end	

hsh = Hash.new()

hsh["color"] = "Black"
hsh["age"] = 20
hsh["school"] = "Angels' Academy Haridwar"
hsh["college"] = "Graphic Era University"

puts "Hash default implementation"

hsh.default = proc do|hash,key|
hash = key+key
end

puts "Hash contents are : #{hsh}"

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

puts "The value of #{ky} is #{hsh[ky]}"

Output

输出量

Hash default implementation
Hash contents are : {"color"=>"Black", "age"=>20, "school"=>"Angels' Academy Haridwar", "college"=>"Graphic Era University"}
Enter the key you want to find:
 animal
The value of animal is #<Proc:[email protected](repl):14>

Explanation:

说明:

In the above code, you can observe that we are trying to set a proc as the default value of the hash object. You can also observe that we are not getting the desired result because you can never set a proc as the default value of any hash with the help of this method. For completing the task, you will always need the default_proc method.

在上面的代码中,您可以观察到我们正在尝试将proc设置为哈希对象的默认值。 您还可以观察到我们没有得到理想的结果,因为您永远无法借助此方法将proc设置为任何哈希的默认值。 为了完成任务,您将始终需要default_proc方法

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

as_hash ruby

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值