获取所有项目在循环中创建,红宝石在轨道上

时间:2023-01-14 12:18:29

Can I get all items been created in loop with ruby on rails?? Here my example:

我可以在轨道上用ruby创建所有项目吗?这是我的例子:

@fb_data_hash = {:key1 => value1, :key2 => value2, :key3 => value3, ...}
@fb_data_hash.each do |key, val|
timeline = get_id_and_content(key, val)
  #timeline will be created like this:
    =>first:  timeline = {:id => "1", :content => "abcd"}
    =>second:  timeline = {:id => "2", :content => "efgh"}
    =>third:  timeline = {:id => "3", :content => "ijkl"}
end

So, I want to get all timeline each time it been created, like this:

所以,我希望每次创建时都获得所有时间轴,如下所示:

alltimelines = [{:id => "1", :content => "abcd"},{:id => "2", :content => "efgh"},{:id => "3", :content => "ijkl"}]

<=>

<=>

alltimelines = [timeline(first),timeline(second),timeline(third)]

But, I don't know how to do that, I tried create an array like this:

但是,我不知道该怎么做,我尝试创建一个这样的数组:

alltimelines = Array.new
alltimelines << timeline

but, it just get one timeline in first. Please help me :)

但是,它首先得到一个时间轴。请帮帮我 :)

3 个解决方案

#1


2  

You can use map for this purpose

您可以使用map来实现此目的

some_hash = { key: "value", other_key: "other_value" }
some_hash.map do |key, val|
  get_id_and_content(key, val)
end
# => equivalent to [ get_id_and_content(:key, "value"), get_id_and_content(:other_key, "other_value") ]

#2


1  

You could simply use Enumerable#collect which will return a new array by running a block on each item of the enumerable, but also it won't destroy or modify the old variable

您可以简单地使用Enumerable#collect,它将通过在枚举的每个项目上运行块来返回一个新数组,但它也不会销毁或修改旧变量

timeline = @fb_data_hash.collect { |key, val| get_id_and_content(key, val) }
# @fb_data_hash is still intact
# timeline has the data that you want

#3


0  

If you really want this format with an array like:

如果你真的想要这种格式的数组如下:

all_timelines = [{:id => "1", :content => "abcd"},{:id => "2", :content => "efgh"},{:id => "3", :content => "ijkl"}]

This code should do the trick (assuming the fact that @fb_data_hash actually contains data) :

这段代码应该可以解决问题(假设@fb_data_hash实际上包含数据):

all_timelines = Array.new
@fb_data_hash.each do |key, val|
  all_timelines.push({:id => key, :content => val})
end

Then you can call your array like that (as an exemple)

然后你可以这样调用你的数组(作为例子)

all_timelines.first # return {:id=>1, :content=>"abcd"}

Hope that is you are looking for.

希望你在寻找。

#1


2  

You can use map for this purpose

您可以使用map来实现此目的

some_hash = { key: "value", other_key: "other_value" }
some_hash.map do |key, val|
  get_id_and_content(key, val)
end
# => equivalent to [ get_id_and_content(:key, "value"), get_id_and_content(:other_key, "other_value") ]

#2


1  

You could simply use Enumerable#collect which will return a new array by running a block on each item of the enumerable, but also it won't destroy or modify the old variable

您可以简单地使用Enumerable#collect,它将通过在枚举的每个项目上运行块来返回一个新数组,但它也不会销毁或修改旧变量

timeline = @fb_data_hash.collect { |key, val| get_id_and_content(key, val) }
# @fb_data_hash is still intact
# timeline has the data that you want

#3


0  

If you really want this format with an array like:

如果你真的想要这种格式的数组如下:

all_timelines = [{:id => "1", :content => "abcd"},{:id => "2", :content => "efgh"},{:id => "3", :content => "ijkl"}]

This code should do the trick (assuming the fact that @fb_data_hash actually contains data) :

这段代码应该可以解决问题(假设@fb_data_hash实际上包含数据):

all_timelines = Array.new
@fb_data_hash.each do |key, val|
  all_timelines.push({:id => key, :content => val})
end

Then you can call your array like that (as an exemple)

然后你可以这样调用你的数组(作为例子)

all_timelines.first # return {:id=>1, :content=>"abcd"}

Hope that is you are looking for.

希望你在寻找。