Hash#shift using default values

栏目: IT技术 · 发布时间: 4年前

内容简介:The 3 main ways to declare a default value to aEach of these 3 methods produces a differentThe

How the way you define your hash default value can generate side effects in your program?

Feb 24 ·3min read

The 3 main ways to declare a default value to a Hash are:

  • by using the Hash.new('default value') method
  • by using the Hash#default= method
  • by using the Hash.new { |h, k| h[k] = 'default value' }

Each of these 3 methods produces a different behavior using the Hash#shift method.

Hash#shift

The hash#shift method removes and returns a pair key/value of a hash

Here, we can see that the initial hash contains a pair foo: :bar .

After the call to hash.shift , the hash is empty and the pair [:foo, :bar] is returned and stored in the pair variable.

Then if we call the hash.shift method on an empty hash nil is returned.

Now that we’re more familiar with the Hash#shift method, let’s see how it behaves when the hash defines a default value.

Hash#default=

Here, we define an empty hash . Then we set the 'val' default value for this hash .

Finally, when we call the hash.shift method we notice that our default value is returned as the hash is empty.

Hash.new(default_value)

Here we define an empty hash using the Hash.new with an argument as default value.

When we call the hash.shift method, we notice the exact same behavior as a hash that sets default value using the Hash#default= method.

Hash.new {}

Here we define an empty hash using the Hash.new with a block that defines the default value.

Then we call the hash.shift method that returns the hash default value.

But a second call to hash.shift returns a pair with nil as key and our default value as value.

Weird..

Not at all. Let’s detail what happens here in the following section.

blocks and Hash#shift

Let’s add few lines to our previous example to understand what’s happens step-by-step

Here, we display the content of our hash after a call to hash.shift and we notice that it contains a nil => 'val' pair.

Then we call hash.shift for the second time and logically this pair is returned.

So, why this pair is inserted in our hash after the first call to hash.shift ?

The answer is simple: because the block passed to Hash.new is executed each time that you code tries to access the default value of our hash .

Indeed, the call to hash.shift forces our hash to execute the block passed to Hash.new as its return value is used as default value of hash — when hash is empty.

But the subtlety here is that k in the |h, k| block arguments is nil .

So, the block is processed as following:

hash[nil] = ‘val’
hash#[]=
'val'

This is why the first call to hash.shift return 'val' and sets the pair { nil => 'val' } in our hash .

So a second call to hash.shift logically returns [nil, 'val'] and not the default value as the hash is not empty at this stage.

Conclusion

When using Hash#shift or any methods that interact with the default value of the hash, be aware if you use a block as default value of the hash.

Indeed, any assignations within this block can generate side effects that can roughly alter the behavior of your app.

Voilà


以上所述就是小编给大家介绍的《Hash#shift using default values》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

电商产品经理宝典:电商后台系统产品逻辑全解析

电商产品经理宝典:电商后台系统产品逻辑全解析

刘志远 / 电子工业出版社 / 2017-10-1 / 49.00元

时至今日,对于产品经理的要求趋向业务型、平台型,甚至产生了细分领域专家。纯粹的前端产品经理(页面、交互)逐渐失去竞争力。而当后台产品经理的视野开始从功能延伸到模块,再延伸到子系统,最后关注整体系统时,就有了把控平台型产品的能力。 《电商产品经理宝典:电商后台系统产品逻辑全解析》围绕“电商后台产品”,从电商的整体产品架构入手,逐步剖析各支撑子系统。通过学习电商产品后台的架构和逻辑,可以让读者从......一起来看看 《电商产品经理宝典:电商后台系统产品逻辑全解析》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具