hyperrjas
hyperrjas

Reputation: 10744

store in instance variable, each block results ruby

This sentence b.followees_by_type("biscuit") return something like:

=> [#<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> ] 

Can return one or more biscuits.

I want add inside a instance variable @biscuits, all biscuits I get in each loop from this block:

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

How can I do it?

Upvotes: 0

Views: 164

Answers (1)

Lars Haugseth
Lars Haugseth

Reputation: 14881

Use Enumerable#flat_map:

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

Upvotes: 1

Related Questions