as_hash ruby_Ruby中带有示例的Hash.default_proc方法

as_hash ruby

Hash.default_proc方法 (Hash.default_proc Method)

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

Method description:

方法说明:

This method is a public instance method that is defined in Ruby library especially for Hash class. This method works in a way that if the hash new method has been invoked with a block then it will return that block when the default_proc() method has been invoked. This method will return nil if the new method has been invoked without passing any block. You will understand the concept in a broader way when you will go through its examples.

此方法是在Ruby库中定义的公共实例方法,特别是针对Hash类。 该方法的工作方式是,如果已使用一个块调用了hash新方法,则当调用default_proc()方法时它将返回该块。 如果在没有传递任何块的情况下调用了新方法,则此方法将返回nil。 当您浏览示例时,将更广泛地理解该概念。

Syntax:

句法:

    Hash_object.default_proc

Argument(s) required:

所需参数:

This method does not require any argument.

此方法不需要任何参数。

Example 1:

范例1:

=begin
  Ruby program to demonstrate default_proc method
=end	

h = Hash.new {|h,k| h[k] = k*k }

puts "default_proc implementation:"   

p = h.default_proc                 
a = []                             

puts "#{p.call(a, 2)}"
puts "The elements in 'a' array are: #{a}" 

Output

输出量

default_proc implementation:
4
The elements in 'a' array are: [nil, nil, 4]

Explanation:

说明:

In the above code, you can observe that we are invoking a new method along with the block and that block is doing nothing rather than returning the double of the element passed. When we are calling the proc p which is containing the default proc value of Hash object along with the index and block value than at the time of printing the array, we are getting the block returned value stored at the index position.

在上面的代码中,您可以观察到我们正在与该块一起调用一个新方法,并且该块什么也不做,而不是返回所传递元素的两倍。 当我们调用proc p时 ,它包含Hash对象的默认proc值以及索引和块值,而不是在打印数组时,它包含存储在索引位置的块返回值。

Example 2:

范例2:

=begin
  Ruby program to demonstrate default_proc method
=end	

h = Hash.new 

puts "default_proc implementation:"   

p = h.default_proc                 
a = []                             

puts "#{p.call(a, 2)}"
puts "The elements in 'a' array are: #{a}"            

Output

输出量

default_proc implementation:
undefined method `call' for nil:NilClass
(repl):12:in `<main>'

Explanation:

说明:

In the above code, you can observe that when the new method is invoked without a block then the variable 'p' is not considered as a proc because the hash default_proc method has returned 'nil'.

在上面的代码中,您可以观察到,在没有块的情况下调用新方法时,变量'p'不被视为proc,因为哈希default_proc方法返回了'nil'

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

as_hash ruby

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值