Hi, Im looking to display my products in a table so that they show up as: X-X-X-X X-X-X-X X-X-X... I have my @products but how can I loop it so that for every 4 (or N) products I can insert a new </tr><tr> to close the previous and open a new row? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 10/16/06, Shai Shefer <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Hi, > > Im looking to display my products in a table so that they show up as: > > X-X-X-X > X-X-X-X > X-X-X... > > I have my @products but how can I loop it so that for every 4 (or N) > products I can insert a new </tr><tr> to close the previous and open a > new row?#in_groups_of http://weblog.rubyonrails.org/2006/3/1/new-in-rails-enumerable-group_by-and-array-in_groups_of <% @products.in_groups_of(4) do |row| %> <tr> <% row.each do |product| %> <td><%= product.name %></td> <% end %> </tr> <% end -%> Matt --> Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Matt Pelletier wrote:> > #in_groups_of > > http://weblog.rubyonrails.org/2006/3/1/new-in-rails-enumerable-group_by-and-array-in_groups_of > > <% @products.in_groups_of(4) do |row| %> > <tr> > <% row.each do |product| %> > <td><%= product.name %></td> > <% end %> > </tr> > <% end -%>So simple! I should have guessed. Thank you. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Oct-17 01:02 UTC
Re: Going through an array in batches...
Hi -- On Mon, 16 Oct 2006, Matt Pelletier wrote:> On 10/16/06, Shai Shefer <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> >> >> Hi, >> >> Im looking to display my products in a table so that they show up as: >> >> X-X-X-X >> X-X-X-X >> X-X-X... >> >> I have my @products but how can I loop it so that for every 4 (or N) >> products I can insert a new </tr><tr> to close the previous and open a >> new row? > > > #in_groups_of > > http://weblog.rubyonrails.org/2006/3/1/new-in-rails-enumerable-group_by-and-array-in_groups_of > > <% @products.in_groups_of(4) do |row| %> > <tr> > <% row.each do |product| %> > <td><%= product.name %></td> > <% end %> > </tr> > <% end -%>This brings back memories: Array#in_chunks_of was one of the first add-on methods I ever wrote :-) There''s also now this in Ruby: require ''enumerator'' array.each_slice(n) do |slice| ... end David -- David A. Black | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---