I want to end up with an array of hashes.
I am starting with an array of codes:
@codes = ['123', '456', '789']
I take each of those codes and hit an API with them and it returns values that I parse into variables in a loop, like so:
@codes.each do |x|
@y = x.get_some_data
@brand = @y[brand]
@size = @y[size]
end
Then I want to put this data into an array of hashes
merged_array = []
final_hash = @codes.map{|code| {:code => code, :brand=> @brand, :size=> @size}
merged_array << final_hash
And in a perfect world, end up with hashes that look like this in the merged_array:
{:code => '123', :brand=> 'nike', :size=> 8 }
{:code => '456', :brand=> 'adidas', :size=> 4 }
{:code => '789', :brand=> 'converse', :size=> 10 }
But when I run my script it maps the codes right, but overwrites the @brand, @size variables and just returns the values of the last loop.
Not sure how to get all my variables into the hashes?
codes.eachsection just put all this logic directly in themapmethod also probably no need for instance variables since you will just discard this data anyway.