How to split a Ruby array into X parts

Posted by macek on Stack Overflow See other posts from Stack Overflow or by macek
Published on 2010-04-23T15:00:30Z Indexed on 2010/04/23 15:03 UTC
Read the original article Hit count: 163

Filed under:
|

I have an array

foo = %w(1 2 3 4 5 6 7 8 9 10)

How can I split or "chunk" this into smaller arrays?

class Array
  def chunk(size)
    # return array of arrays
  end
end

foo.chunk(3)
# => [[1,2,3],[4,5,6],[7,8,9],[10]]

© Stack Overflow or respective owner

Related posts about ruby

Related posts about arrays