# Grouping elements of an array?

**URL:** <https://rubytalk.org/t/grouping-elements-of-an-array/34289>\
**Category:** ruby-talk\
**Created:** [8 January 2007 05:35 UTC](https://rubytalk.org/t/grouping-elements-of-an-array/34289 "2007-01-08T05:35:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sam\_Kong](https://avatars.discourse-cdn.com/v4/letter/s/ad7895/32.png) [@Sam\_Kong](https://rubytalk.org/u/Sam_Kong)\
**Post date:** [8 January 2007 05:35 UTC](https://rubytalk.org/t/grouping-elements-of-an-array/34289/1 "2007-01-08T05:35:05Z")

</div>

Hello,

What's the best way to do the following?

[1,2,3,4,5,6,7,8,9] =\> [[1,2,3],[4,5,6],[7,8,9]]

I want to transform the array into an array of arrays with fixed number  
of elements.  
I think there should be a method but I can't find it.

I can iterate the elements using a counter to divide but it's so  
boring.

Thanks in advance.

Sam

---

<div class="post-metadata">

**Author:** ![Jeremy\_McAnally1](https://avatars.discourse-cdn.com/v4/letter/j/eb8c5e/32.png) [@Jeremy\_McAnally1](https://rubytalk.org/u/Jeremy_McAnally1)\
**Post date:** [8 January 2007 05:50 UTC](https://rubytalk.org/t/grouping-elements-of-an-array/34289/2 "2007-01-08T05:50:41Z")

</div>

class Array  
&nbsp;&nbsp;def group\_in(how\_many)  
&nbsp;&nbsp;&nbsp;ret\_array =   
&nbsp;&nbsp;&nbsp;new\_array =   
&nbsp;&nbsp;&nbsp;self.each {|elem| how\_many.times { new\_array \<\< self.shift };  
ret\_array \<\< new\_array; new\_array = ; }  
&nbsp;&nbsp;&nbsp;ret\_array  
&nbsp;&nbsp;end  
end

x = [1,2,3,4,5,6,7,8,9]  
x = x.group\_in(3)  
require 'pp'  
pp x

> > [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

That should do it. 🙂 Not sure if it's the best way, but it will work.

--Jeremy

> **···**
>
> On 1/8/07, Sam Kong \<sam.s.kong@gmail.com\> wrote:
> 
> > Hello,
> > 
> > What's the best way to do the following?
> > 
> > [1,2,3,4,5,6,7,8,9] =\> [[1,2,3],[4,5,6],[7,8,9]]
> > 
> > I want to transform the array into an array of arrays with fixed number  
> > of elements.  
> > I think there should be a method but I can't find it.
> > 
> > I can iterate the elements using a counter to divide but it's so  
> > boring.
> > 
> > Thanks in advance.
> > 
> > Sam
> 
> --  
> My free Ruby e-book:  
> [http://www.humblelittlerubybook.com/book/](http://www.humblelittlerubybook.com/book/)
> 
> My blogs:
> 
> > **[Site not found · DreamHost](http://www.mrneighborly.com/)**
> >
> > The owner of this domain has not yet uploaded their website.
> 
> [http://www.rubyinpractice.com/](http://www.rubyinpractice.com/)

---

<div class="post-metadata">

**Author:** ![Farrel\_Lifson](https://avatars.discourse-cdn.com/v4/letter/f/3da27b/32.png) [@Farrel\_Lifson](https://rubytalk.org/u/Farrel_Lifson)\
**Post date:** [8 January 2007 05:56 UTC](https://rubytalk.org/t/grouping-elements-of-an-array/34289/3 "2007-01-08T05:56:26Z")

</div>

requre 'enumerator'

[1,2,3,4,5,6,7,8,9].enum\_slice(3).inject(){|array,slice| array \<\< slice}

Farrel

> **···**
>
> On 08/01/07, Sam Kong \<sam.s.kong@gmail.com\> wrote:
> 
> > Hello,
> > 
> > What's the best way to do the following?
> > 
> > [1,2,3,4,5,6,7,8,9] =\> [[1,2,3],[4,5,6],[7,8,9]]
> > 
> > I want to transform the array into an array of arrays with fixed number  
> > of elements.  
> > I think there should be a method but I can't find it.
> > 
> > I can iterate the elements using a counter to divide but it's so  
> > boring.
> > 
> > Thanks in advance.
> > 
> > Sam

---

<div class="post-metadata">

**Author:** ![Shiwei\_Zhang](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@Shiwei\_Zhang](https://rubytalk.org/u/Shiwei_Zhang)\
**Post date:** [8 January 2007 07:00 UTC](https://rubytalk.org/t/grouping-elements-of-an-array/34289/4 "2007-01-08T07:00:19Z")

</div>

FYI. B is what you expect:  
A=[1,2,3,4,5,6,7,8,9];B=;  
3.times {B\<\<A.slice!(0..2);}

Sam Kong wrote:

> **···**
>
> > Hello,
> > 
> > What's the best way to do the following?
> > 
> > [1,2,3,4,5,6,7,8,9] =\> [[1,2,3],[4,5,6],[7,8,9]]
> > 
> > I want to transform the array into an array of arrays with fixed number  
> > of elements.  
> > I think there should be a method but I can't find it.
> > 
> > I can iterate the elements using a counter to divide but it's so  
> > boring.
> > 
> > Thanks in advance.
> > 
> > Sam

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [8 January 2007 06:02 UTC](https://rubytalk.org/t/grouping-elements-of-an-array/34289/5 "2007-01-08T06:02:31Z")

</div>

Farrel Lifson wrote:

> > What's the best way to do the following?
> > 
> > [1,2,3,4,5,6,7,8,9] =\> [[1,2,3],[4,5,6],[7,8,9]]

...

> [1,2,3,4,5,6,7,8,9].enum\_slice(3).inject(){|array,slice| array \<\< slice}

Alternately,

[1,2,3,4,5,6,7,8,9].enum\_for(:each\_slice,3).to\_a  
=\> [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

> **···**
>
> > On 08/01/07, Sam Kong \<sam.s.kong@gmail.com\> wrote:
> 
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [8 January 2007 07:25 UTC](https://rubytalk.org/t/grouping-elements-of-an-array/34289/6 "2007-01-08T07:25:12Z")

</div>

Farrel Lifson wrote:

> **···**
>
> > On 08/01/07, Sam Kong \<sam.s.kong@gmail.com\> wrote:  
> > \> Hello,  
> > \>  
> > \> What's the best way to do the following?  
> > \>  
> > \> [1,2,3,4,5,6,7,8,9] =\> [[1,2,3],[4,5,6],[7,8,9]]  
> > \>  
> > \> I want to transform the array into an array of arrays with fixed number  
> > \> of elements.  
> > \> I think there should be a method but I can't find it.  
> > \>  
> > \> I can iterate the elements using a counter to divide but it's so  
> > \> boring.  
> > \>  
> > \> Thanks in advance.  
> > \>  
> > \> Sam  
> > \>  
> > \>  
> > \>
> > 
> > requre 'enumerator'
> > 
> > [1,2,3,4,5,6,7,8,9].enum\_slice(3).inject(){|array,slice| array \<\< slice}
> > 
> > Farrel
> 
> requre 'enumerator'  
> [1, 2, 3, 4, 5, 6, 7, 8].enum\_slice(3).to\_aj
> 
> Without "require":  
> [1, 2, 3, 4, 5, 6, 7, 8].inject([]){|a,e|  
> &nbsp;&nbsp;a \<\< if a.last.size==3; a.last \<\< e; a}

---

<div class="post-metadata">

**Author:** ![Sam\_Kong](https://avatars.discourse-cdn.com/v4/letter/s/ad7895/32.png) [@Sam\_Kong](https://rubytalk.org/u/Sam_Kong)\
**Post date:** [8 January 2007 08:12 UTC](https://rubytalk.org/t/grouping-elements-of-an-array/34289/7 "2007-01-08T08:12:12Z")

</div>

Hi William,

William James wrote:

> Without "require":  
> [1, 2, 3, 4, 5, 6, 7, 8].inject([]){|a,e|  
> &nbsp;&nbsp;a \<\< if a.last.size==3; a.last \<\< e; a}

This looks really clever.  
I like it.

Sam
