修改默认的哈希值 [英] Modifying the default hash value

查看:149
本文介绍了修改默认的哈希值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

  h = Hash.new(['alright'])$ b允许您为散列定义默认值: $ bh ['meh']#=> [好吧] 

显示哈希值时会显示赋值,但修改的默认值不。 'bad'

  h ['good'] = [ '''''''''''$'$'''''''''''''' {good=> [fine,dandy]} 

<$ c $如果我们明确要求,那么c>'bad'会显示出来。

  h ['bad']# => [好吧,不高兴] 

为什么修改后的默认值在显示时不显示散列?

解决方案

散列的默认值不会像预期的那样工作。当你说 h [k] 时,过程如下:我们有一个 k 键,返回它的值。

  • 如果我们有一个Hash的默认值,返回默认值。 / li>
  • 如果我们有一个提供默认值的模块,请执行该模块并返回它的返回值。



    <注意(2)和(3)对于在Hash中插入 k 没有任何意见。默认值本质上将 h [k] 转换为:

      h .has_key?(k)? h [k]:the_default_value 

    因此,只需访问一个不存在的键并获取默认值'

    此外,以下任何形式:

     

    Hash.new([...])
    #或
    Hash.new({...})

    几乎总是一个错误,因为您将为所有默认值共享完全相同的默认数组或哈希值。例如,如果你这样做:

      h = Hash.new(['a'])
    h [: k] .push('b')

    然后 h [:i] , h [:j] ,...将全部返回 ['a','b'] ,这很少是你想要的。



    我认为你正在寻找块形式的默认值

      h = Hash.new {| h,k | h [k] = ['好吧']} 

    这会做两件事:


    1. 访问不存在的键会将该键添加到哈希,并将提供的数组作为其值。

    2. 所有默认值都是不同的对象,所以更改一个不会改变其余的。


    Ruby lets you define default values for hashes:

    h=Hash.new(['alright'])
    h['meh'] # => ["alright"]
    

    Assignment of a value shows up when displaying the hash, but a modified default does not. Where's 'bad'?

    h['good']=['fine','dandy']
    h['bad'].push('unhappy')
    h # => {"good"=>["fine", "dandy"]}
    

    'bad' shows up if we explicitly ask.

    h['bad'] # => ["alright", "unhappy"]
    

    Why does the modified default value not show up when displaying the hash?

    解决方案

    Hash's default value doesn't work like you're expecting it to. When you say h[k], the process goes like this:

    1. If we have a k key, return its value.
    2. If we have a default value for the Hash, return that default value.
    3. If we have a block for providing default values, execute the block and return its return value.

    Note that (2) and (3) say nothing at all about inserting k into the Hash. The default value essentially turns h[k] into this:

    h.has_key?(k) ? h[k] : the_default_value
    

    So simply accessing a non-existant key and getting the default value back won't add the missing key to the Hash.

    Furthermore, anything of the form:

    Hash.new([ ... ])
    # or
    Hash.new({ ... })
    

    is almost always a mistake as you'll be sharing exactly the same default Array or Hash for for all default values. For example, if you do this:

    h = Hash.new(['a'])
    h[:k].push('b')
    

    Then h[:i], h[:j], ... will all return ['a', 'b'] and that's rarely what you want.

    I think you're looking for the block form of the default value:

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

    That will do two things:

    1. Accessing a non-existent key will add that key to the Hash and it will have the provided Array as its value.
    2. All of the default values will be distinct objects so altering one will not alter the rest.

    这篇关于修改默认的哈希值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆