Ruby method Array#<< not updating the array in hash
Posted
by Mladen Jablanovic
on Stack Overflow
See other posts from Stack Overflow
or by Mladen Jablanovic
Published on 2010-03-31T11:49:50Z
Indexed on
2010/03/31
11:53 UTC
Read the original article
Hit count: 213
Inspired by http://stackoverflow.com/questions/2552363/how-can-i-marshal-a-hash-with-arrays I wonder what's the reason that Array#<<
won't work properly in the following code:
h = Hash.new{Array.new}
#=> {}
h[0]
#=> []
h[0] << 'a'
#=> ["a"]
h[0]
#=> [] # why?!
h[0] += ['a']
#=> ["a"]
h[0]
#=> ["a"] # as expected
Does it have to do with the fact that <<
changes the array in-place, while Array#+
creates a new instance?
© Stack Overflow or respective owner