Initializing hashes

后端 未结 5 1501
傲寒
傲寒 2021-02-07 10:25

I frequently write something like this:

a_hash[\'x\'] ? a_hash[\'x\'] += \' some more text\' : a_hash[\'x\'] = \'first text\'

There ought to be

相关标签:
5条回答
  • 2021-02-07 10:37

    In case you do not want to mess with the default= value (existing hash), you can shorten your code by using fetch(key [, default]) as a look-up with a default value:

    a_hash['x'] = a_hash.fetch('x', 'first_text') + ' some more text'
    
    0 讨论(0)
  • 2021-02-07 10:38

    The constructor of Hash, in its first argument, have a default value for the keys. This way

    >> a_hash = Hash.new "first text"
    => {}
    >> a_hash['a']
    => "first text"
    >> a_hash['b'] += ", edit"
    => "first text, edit"
    
    0 讨论(0)
  • 2021-02-07 10:54

    Since you are using the hash to collect strings, I assume you simply want to make sure that you don't get an error when appending. Therefore, I'd make the hash default the empty string. Then you can append without error, no matter the hash key.

    a_hash = Hash.new {|h,k| h[k]=""}
    
    texts = ['first text', ' some more text']
    
    texts.each do |text|
      a_hash['x'] << text
    end
    
    puts a_hash['x'] #=> 'first text some more text'
    
    0 讨论(0)
  • 2021-02-07 10:59

    There are two ways to create initial values with for a Hash.

    One is to pass a single object in to Hash.new. This works well in many situations, especially if the object is a frozen value, but if the object has internal state, this may have unexpected side-effects. Since the same object is shared between all keys without an assigned value, modifying the internal state for one will show up in all.

    a_hash = Hash.new "initial value"
    a_hash['a'] #=> "initial value"
    # op= methods don't modify internal state (usually), since they assign a new
    # value for the key.
    a_hash['b'] += ' owned by b' #=> "initial value owned by b"
    # other methods, like #<< and #gsub modify the state of the string
    a_hash['c'].gsub!(/initial/, "c's")
    a_hash['d'] << " modified by d"
    a_hash['e'] #=> "c's value modified by d"
    

    Another initialization method is to pass Hash.new a block, which is invoked each time a value is requested for a key that has no value. This allows you to use a distinct value for each key.

    another_hash = Hash.new { "new initial value" }
    another_hash['a'] #=> "new initial value" 
    # op= methods still work as expected
    another_hash['b'] += ' owned by b'
    # however, if you don't assign the modified value, it's lost,
    # since the hash rechecks the block every time an unassigned key's value is asked for
    another_hash['c'] << " owned by c" #=> "new initial value owned by c"
    another_hash['c'] #=> "new initial value"
    

    The block is passed two arguments: the hash being asked for a value, and the key used. This gives you the option of assigning a value for that key, so that the same object will be presented each time a particular key is given.

    yet_another_hash = Hash.new { |hash, key| hash[key] = "#{key}'s initial value" }
    yet_another_hash['a'] #=> "a's initial value"
    yet_another_hash['b'] #=> "b's initial value"
    yet_another_hash['c'].gsub!('initial', 'awesome')
    yet_another_hash['c'] #=> "c's awesome value"
    yet_another_hash #=> { "a" => "a's initial value", "b" => "b's initial value", "c" => "c's awesome value" }
    

    This last method is the one I most often use. It's also useful for caching the result of an expensive calculation.

    0 讨论(0)
  • 2021-02-07 11:02

    You can specify the initial value when you create your hash:

    a_hash = { 'x' => 'first text' }
    // ...
    a_hash['x'] << ' some more text'
    
    0 讨论(0)
提交回复
热议问题