a
是一个哈希。s
是一个数组,我想在其中a
迭代地推送哈希。这个想法是独立地保留每个迭代推送的哈希值。这是我尝试过的。
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}]
以上并没有给我解决方案:更改a
会更改先前推送的数组内元素中的值。推a
到s
两次后,我a[:b]
从2
变为3
,所有元素都反映了变化。请对此提出建议。