# Collections in groups?

**URL:** <https://www.ruby-forum.com/t/collections-in-groups/112842>\
**Category:** Rails\
**Created:** [August 23, 2007, 8:22pm UTC](https://www.ruby-forum.com/t/collections-in-groups/112842 "2007-08-23T20:22:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jables](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@Jables](https://www.ruby-forum.com/u/Jables)\
**Post date:** [August 23, 2007, 8:22pm UTC](https://www.ruby-forum.com/t/collections-in-groups/112842/1 "2007-08-23T20:22:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jables](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@Jables](https://www.ruby-forum.com/u/Jables)\
**Post date:** [August 23, 2007, 9:28pm UTC](https://www.ruby-forum.com/t/collections-in-groups/112842/2 "2007-08-23T21:28:51Z")

</div>

On 8/23/07, Jables [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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, '&nbsp;') do |item| -%>
        <tr class="<%= cycle("item0","item1") %>">
          <% item.each do |item| next unless

```

item.respond\_to?(:product\_name) -%\>  
  
\<%= item.product\_name %\>  
  
\<% end -%\>  
  
\<% end -%\>

---

<div class="post-metadata">

**Author:** ![Jables](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@Jables](https://www.ruby-forum.com/u/Jables)\
**Post date:** [August 24, 2007, 1:44am UTC](https://www.ruby-forum.com/t/collections-in-groups/112842/3 "2007-08-24T01:44:20Z")

</div>

Thanks Adam.
