Here is a code
irb(main):085:0> h = Hash.new([])
=> {}
irb(main):086:0> h['a'] = 'sdfds'
=> "sdfds"
irb(main):087:0> h
=> {"a"=>"sdfds"}
irb(main):088:0> h['b'].push(h['a'] )
=> ["sdfds"]
irb(main):089:0> h
=> {"a"=>"sdfds"}
irb(main):090:0> h['b']
=> ["sdfds"]
irb(main):091:0> h['c']
=> ["sdfds"]
irb(main):092:0> h
=> {"a"=>"sdfds"}
What I was trying to do was make h[b] act like a usual Array. However what happens is h[b] and h[c] now have a new default value. I expected h[b] to have this new value but seems like push doesn't actually push to a non existent value.
and then printing h actually shows just h[a]
why is this? And this is really not worth a try although ruby is used widely but these are the kind of peculiar behaviors that might not be preferred. and it varies person to person.
UPDATE However the right behavior isn't showing anything fishy:
irb(main):104:0> h = Hash.new([])
=> {}
irb(main):105:0> h['a'] = [1,2,3,'dsfds']
=> [1, 2, 3, "dsfds"]
irb(main):106:0> h['b'] += h['a']
=> [1, 2, 3, "dsfds"]
irb(main):107:0> h
=> {"a"=>[1, 2, 3, "dsfds"], "b"=>[1, 2, 3, "dsfds"]}
Another unexpected and confusing behavior
irb(main):093:0> h = [1,2,3]
=> [1, 2, 3]
irb(main):094:0> h.shift(0)
=> []
irb(main):095:0> h
=> [1, 2, 3]
irb(main):096:0> h.unshift(0)
=> [0, 1, 2, 3]
irb(main):097:0> h.shift(10)
=> [0, 1, 2, 3]
irb(main):098:0> h.shift(90)
=> []
irb(main):099:0> h
=> []
irb(main):100:0> h = [1,2,3]
=> [1, 2, 3]
irb(main):101:0> h.shift(100)
=> [1, 2, 3]
irb(main):102:0> h
=> []
irb(main):103:0> h.shift(90)
=> []
Im not even gona ask the question here if it doesnt surprise you enough but I would like to read some explanations on such weird behavior. Makes me think if I should use it in production environemnt at all.