Is there a way to traverse an array and perform an action (like collect) in sets? I am trying to use an array to populate a table with three cells per row, so I need to pop in sets of three, then create a new row. Thanks, JB --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 8/23/07, Jables <brun3797-Meo6Lv8EUjg3uPMLIKxrzw@public.gmane.org> wrote:> > Is there a way to traverse an array and perform an action (like > collect) in sets? I am trying to use an array to populate a table > with three cells per row, so I need to pop in sets of three, then > create a new row.you need the method in_groups_of. Here''s how I use it <table> <% @products.in_groups_of(2, '' '') do |item| -%> <tr class="<%= cycle("item0","item1") %>"> <% item.each do |item| next unless item.respond_to?(:product_name) -%> <td> <%= item.product_name %> </td> <% end -%> </tr> <% end -%> </table> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks Adam. On Aug 23, 12:27 pm, "Adam Cohen" <bionicboo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 8/23/07, Jables <brun3...-Meo6Lv8EUjg3uPMLIKxrzw@public.gmane.org> wrote: > > > > > Is there a way to traverse an array and perform an action (like > > collect) in sets? I am trying to use an array to populate a table > > with three cells per row, so I need to pop in sets of three, then > > create a new row. > > you need the method in_groups_of. Here''s how I use it > > <table> > <% @products.in_groups_of(2, '' '') do |item| -%> > <tr class="<%= cycle("item0","item1") %>"> > <% item.each do |item| next unless > item.respond_to?(:product_name) -%> > <td> > <%= item.product_name %> > </td> > <% end -%> > </tr> > <% end -%> > </table>--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---