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.
我正在尝试在下面的哈希@data中创建一个动态循环,似乎无法弄明白。我正在使用google api创建一个带注释的timeline-for-rails来自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
散列@data中的数组必须是动态的i:e必须通过循环生成日期编号,并且产品和编号的名称也是动态的。我将尝试在下面的循环中给出一个例子
@numdeployed is a number and comes from a table in the db i should be generated by the loop
@numdeployed是一个数字,来自db中的表,我应该由循环生成
@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
希望有人可以帮忙。谢谢
1 个解决方案
#1
3
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
.
然而,在你的@data中你需要很多对。
#1
3
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
.
然而,在你的@data中你需要很多对。