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
,所有的元素體現了變化。對此的建議請。
本質DUP的[如何複製在Ruby中的哈希?](HTTP:/ /stackoverflow.com/questions/4157399/how-do-i-copy-a-hash-in-ruby) – dbenhur