a
is a hash. s
is an array where I want to push the hash a
iteratively. The idea is to retain the value of each iteratively pushed hash independently. Here is what I tried.
a = {:a=> 1, :b=>2}
s = []
s << a
s << a # => [{:b=>2, :a=>1}, {:b=>2, :a=>1}]
a[:b] = 3
s # => [{:b=>3, :a=>1}, {:b=>3, :a=>1}]
t = []
t.push(a) # => [{:b=>3, :a=>1}]
t.push(a) # => [{:b=>3, :a=>1}, {:b=>3, :a=>1}]
a[:b] = 4
t # => [{:b=>4, :a=>1}, {:b=>4, :a=>1}]
The above doesn't give me the solution: Changing a
changes the values in the elements inside the array which were previously pushed. After pushing a
to s
twice, I changed a[:b]
from 2
to 3
, and all the elements reflected the change. Suggestion for this please.