I'm trying to modify a copy of an array without changing the original array. It's an array of hashes, so to to make an "all new" copy of the array I use:
foo = [ { :a => "aaaaaa" } ]
foocopy = foo.map { |h| h.dup }
I want to append some data to a string in the hash in the copy.
It works fine if I use =
and +
:
foocopy.first[:a] = foocopy.first[:a] + "bbbbb"
foo
=> [{:a=>"aaaaaa"}] # original unchanged as expected
foocopy
=> [{:a=>"aaaaaabbbbb"}]
However if I use <<
it modified BOTH the copy and the original:
foocopy.first[:a] << "cccccc"
foo
=> [{:a=>"aaaaaacccccc"}] # ORIGINAL got changed too
foocopy
=> [{:a=>"aaaaaacccccc"}]
Is that a bug in Ruby?