I was experimenting with Ruby's default Hash values in v2.3.7. I was surprised by some output I got in a simple test case, and I was wondering what was going on behind the scenes that would explain it.
foo = Hash.new({x: 0, y: 0}) # provide a default value
foo['bar'][:x] += 1 # expect to add to the default value
foo # outputs `{}` ?! expected {'bar'=>{:x=>1,:y=>0}}
foo['bar'] # outputs `{:x=>1, :y=>0}` as expected
Why is it that foo
appears to be empty on line 3? I expected output like {'bar'=>{:x=>1,:y=>0}}
. Am I missing something super basic about why this is happening? foo.empty?
returns true
, but foo['bar']
produces output.
Is this a bug?