I'm confused about the way an array is updated when I loop through it. Here's a made up example that shows the behaviour.
people = [{"name"=>"Edward", "age" =>"43", "height"=>"tallish"},
{"name"=>"Ralph", "age" =>"40", "height"=>"medium heigth"},
{"name"=>"George", "age" =>"35", "height"=>"very tall"},
{"name"=>"Mark", "age" =>"25", "height"=>"short"}]
numbers = ["1","3","26"]
new_array = []
numbers.each do |number|
people.each do |person|
person["name"] = person["name"] +" "+ number
new_array << person
end
end
At the end of that new_array is
[{"name"=>"Edward 1 3 26", "age"=>"43", "height"=>"tallish"},
{"name"=>"Ralph 1 3 26", "age"=>"40", "height"=>"medium heigth"},
{"name"=>"George 1 3 26", "age"=>"35", "height"=>"very tall"},
{"name"=>"Mark 1 3 26", "age"=>"25", "height"=>"short"},
{"name"=>"Edward 1 3 26", "age"=>"43", "height"=>"tallish"},
{"name"=>"Ralph 1 3 26", "age"=>"40", "height"=>"medium heigth"},
{"name"=>"George 1 3 26", "age"=>"35", "height"=>"very tall"},
{"name"=>"Mark 1 3 26", "age"=>"25", "height"=>"short"},
{"name"=>"Edward 1 3 26", "age"=>"43", "height"=>"tallish"},
{"name"=>"Ralph 1 3 26", "age"=>"40", "height"=>"medium heigth"},
{"name"=>"George 1 3 26", "age"=>"35", "height"=>"very tall"},
{"name"=>"Mark 1 3 26", "age"=>"25", "height"=>"short"}]
Each person appears three times, which I what I expected and wanted. BUT their name is the same each time. I expected name to be "Edward 1" the first time, then "Edward 1 3" and finally "Edward 1 3 26"
What's going on here? I thought the loop would append each separate hash onto new_array, rather than 3 all the same.