danynl danynl - 10 months ago 44
Ruby Question

ruby - Inserting multiple hashes into an array in ruby

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.

Answer Source

You're saving the same hash in two different locations of the array. Think of Ruby as adding object references instead of copying the object each time you nest it inside another thing.

Do do this with different values, you might want to create a new one each time:

part_array = [ ]
part_array << {
  # ... Hash entry

There are legitimate reasons why you might want to put the same thing in an array twice, it could be a way of saving memory when using a large data structure.

As a note, Ruby tends to strongly recommend variable names like part_array, all lower-case. Likewise, for Hash keys, Symbols are often preferred since they're inexpensive internally.