How to split (chunk) a Ruby array into parts of X elements? [duplicate]

Solution 1:

Take a look at Enumerable#each_slice:

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

Solution 2:

If you're using rails you can also use in_groups_of:

foo.in_groups_of(3)