I want to insert multiple hashes into an array which will create an array of hashes. But every time I add a new hash to the array, it would overwrite the previous ones. Any idea what is going on?
partArray = []
partHash = {}
partHash["name"] = "Item1"
partHash["owner"] = "Item1"
#Insert first hash into array
partArray << partHash
puts partArray
#new set of key, value pairs
#to be appended to array
partHash["name"] = "Item2"
partHash["owner"] = "Item2"
#Append second hash into array
partArray << partHash
puts partArray
output :
{"name"=>"Item1", "owner"=>"Item1"}
new Array is :
{"name"=>"Item2", "owner"=>"Item2"}
{"name"=>"Item2", "owner"=>"Item2"}
I'm not sure why the values in the first hash were overwritten. Any help is appreciated.
p partArray.map(&:object_id)