存储在实例变量中,每个块都会产生ruby

时间:2012-06-22 17:06:24

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.1

这句话b.followees_by_type("biscuit")返回类似:

=> [#<Biscuit _id: 4fdf5aa11d41c829ea000020, _type: "Biscuit", created_at: 2012-06-18 16:43:13 UTC, title: "biscuit biscuit", is_complete: true>, #<Biscuit _id: 4fdf5aa11d41c829ea000021, _type: "Biscuit", created_at: 2012-06-18 17:40:10 UTC, title: "biscuit2 biscuit2", is_complete: true> ] 

可以退回一个或多个饼干。

我想在实例变量@biscuits中添加,我从这个块的每个循环中得到的所有饼干:

boards.each do |b| 
  b.followees_by_type("biscuit")
end

我该怎么做?

1 个答案:

答案 0 :(得分:1)

使用Enumerable#flat_map

@biscuits = boards.flat_map {|b| b.followees_by_type('biscuit') }