Reputation: 121
I'm trying to create a dynamic loop within the hash @data below and can't really seem to figure it out. I'm creating an annotatedtimeline-for-rails using the google api from here https://github.com/mcommons/annotatedtimeline-for-rails.
The array within the hash @data has to be dynamic i:e the day number has to be generated by a loop and the name of the product and number are dynamic as well. I'll try to give an example in the loop below
@numdeployed is a number and comes from a table in the db i should be generated by the loop
@data{
begin loop
i.day.ago.to_date => { :foo=>@numdeployed, :bar=>@numdeployed, :barbaz=>@numdeployed, :foobar=>@numdeployed },
end loop
}
The Original Data Hash looks like this
@data = {
1.day.ago.to_date => { :foo=>10, :bar=>40, :barbaz=>10, :foobar=>40 },
2.day.ago.to_date => { :foo=>10, :bar=>40, :barbaz=>10,:foobar=>40 },
3.day.ago.to_date => { :foo=>10, :bar=>40, :barbaz=>10,:foobar=>40 },
4.day.ago.to_date => { :foo=>10, :bar=>40, :barbaz=>10,:foobar=>40 },
5.day.ago.to_date => { :foo=>10, :bar=>40, :barbaz=>10,:foobar=>40 }
}
hope someone can help. Thanks
Upvotes: 0
Views: 404
Reputation: 434635
Are you looking for something like this?
@data = Hash[
n.times.map do |i|
[ (i + 1).day.ago.to_date, { :foo => 10, :bar => 40, :barbaz => 10, :foobar => 40 } ]
end
]
The n
is however many pairs you want in your @data
.
Upvotes: 3