Why does clearing my hash also clear my hash array?
ruby-1.9.2-p180 :154 > a = []
=> []
ruby-1.9.2-p180 :154 > h = {:test => "test"}
=> {:test=>"test"}
ruby-1.9.2-p180 :155 > a << h
=> [{:test=>"test"}]
ruby-1.9.2-p180 :156 > h.clear
=> {}
ruby-1.9.2-p180 :157 > a
=> [{}]
I am very confused, especially since I can change the hash elements without affecting the array. But when I clear the hash, the array is updated and cleared of the contents of the hash. Can someone explain?
+3
2 answers