This is an old question, but anyway, to write how I would do it in Ruby:
class Array def groups_of_size(n) Enumerator.new do |yielder| if self.empty? yielder.yield([]) else self.drop(1).combination(n-1).map { |vs| [self.first] + vs }.each do |values| (self - values).groups_of_size(n).each do |group| yielder.yield([values] + group) end end end end end end
I use an enumerator because the output can grow very quickly, strict output (like an array) will not be useful. Usage example:
>> pp [0, 1, 2, 3, 4, 5].groups_of_size(3).to_a => [[[0, 1, 2], [3, 4, 5]], [[0, 1, 3], [2, 4, 5]], [[0, 1, 4], [2, 3, 5]], [[0, 1, 5], [2, 3, 4]], [[0, 2, 3], [1, 4, 5]], [[0, 2, 4], [1, 3, 5]], [[0, 2, 5], [1, 3, 4]], [[0, 3, 4], [1, 2, 5]], [[0, 3, 5], [1, 2, 4]], [[0, 4, 5], [1, 2, 3]]]