# Partitioning an array

**URL:** <https://rubytalk.org/t/partitioning-an-array/64448>\
**Category:** ruby-talk\
**Created:** [25 December 2011 00:28 UTC](https://rubytalk.org/t/partitioning-an-array/64448 "2011-12-25T00:28:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jake\_S](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@Jake\_S](https://rubytalk.org/u/Jake_S)\
**Post date:** [25 December 2011 00:28 UTC](https://rubytalk.org/t/partitioning-an-array/64448/1 "2011-12-25T00:28:05Z")

</div>

I'm looking for a method that takes an array and returns its elements  
grouped together in arrays of length 2, with the array at the very end  
having a length of 1 or two.

For example:  
group [1,2,3,4,5,6] #=\> [[1,2],[3,4],[5,6]]  
group ['f','o','o'] #=\> [['f','o'],['o']]

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Eric\_Christopherson](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/eric_christopherson/32/1849_2.png) [@Eric\_Christopherson](https://rubytalk.org/u/Eric_Christopherson)\
**Post date:** [25 December 2011 01:24 UTC](https://rubytalk.org/t/partitioning-an-array/64448/2 "2011-12-25T01:24:53Z")

</div>

def group(ary)  
&nbsp;&nbsp;ary.each\_slice(2).to\_a  
end

> **···**
>
> On Dec 24, 2011 6:29 PM, "Jake S." \<jakesmmrs2@gmail.com\> wrote:
> 
> > I'm looking for a method that takes an array and returns its elements  
> > grouped together in arrays of length 2, with the array at the very end  
> > having a length of 1 or two.
> > 
> > For example:  
> > group [1,2,3,4,5,6] #=\> [[1,2],[3,4],[5,6]]  
> > group ['f','o','o'] #=\> [['f','o'],['o']]
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
