7

Code example below. Calling append on a hash value returns correctly but the hash itself doesn't behave as I would expect.

ruby-1.9.2-p290 :037 >   r = {}
 => {} 
ruby-1.9.2-p290 :038 > r.default = []
 => [] 
ruby-1.9.2-p290 :039 > r["c"] << 1
 => [1] 
ruby-1.9.2-p290 :040 > r["c"] 
 => [1] 
ruby-1.9.2-p290 :041 > r
 => {} 
ruby-1.9.2-p290 :042 > r.empty?
 => true 
brahn
  • 12,096
  • 11
  • 39
  • 49
zerocap
  • 93
  • 3

2 Answers2

8

from the doc on default=:

Sets the default value, the value returned for a key that does not exist in the hash. It is not possible to set the default to a Proc that will be executed on each key lookup.

So you can use this:

irb(main):037:0> h = Hash.new { |hash, key| hash[key] = [] }
=> {}
irb(main):038:0> h[1] << "2"
=> ["2"]
irb(main):039:0> h
=> {1=>["2"]}

also you can use default_proc:

irb(main):047:0> h = {}
irb(main):047:0> h.default_proc =  proc {|hash, key| hash[key] = []}
=> #<Proc:0x23939a0@(irb):47> 
irb(main):049:0> h[1] << 3
=> [3]
irb(main):050:0> h
=> {1=>[3]}
Vasiliy Ermolovich
  • 24,459
  • 5
  • 79
  • 77
4

It seems ruby hash uses r.default as default value for any r[<unassigned-key>]. So a single value is changed even if you specify different un-assigned keys. Please see below:

irb(main):001:0> r = {}
=> {}
irb(main):002:0> r.default = []
=> []
irb(main):003:0> r["c"] << 1
=> [1]
irb(main):004:0> r["c"]
=> [1]
irb(main):005:0> r["b"] << 2
=> [1, 2]
irb(main):006:0> r["b"]
=> [1, 2]
irb(main):007:0> r["c"]
=> [1, 2]
irb(main):010:0> r.default
=> [1, 2]
irb(main):008:0> r
=> {}
irb(main):009:0> r.empty?
=> true
ShadyKiller
  • 700
  • 8
  • 16