Partitioning an array

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/.

def group(ary)
  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/\.