class Array
def in_groups(number, fill_with = nil, &block)
["4", "5"]
["1", "2", "3"]
%w(1 2 3 4 5 6 7).in_groups(3, false) {|group| p group}
["8", "9", "10", " "]
["5", "6", "7", " "]
["1", "2", "3", "4"]
%w(1 2 3 4 5 6 7 8 9 10).in_groups(3, ' ') {|group| p group}
["8", "9", "10", nil]
["5", "6", "7", nil]
["1", "2", "3", "4"]
%w(1 2 3 4 5 6 7 8 9 10).in_groups(3) {|group| p group}
remaining slots with +fill_with+ unless it is +false+.
Splits or iterates over the array in +number+ of groups, padding any
def in_groups(number, fill_with = nil, &block) # size.div number gives minor group size; # size % number gives how many objects need extra accommodation; # each group hold either division or division + 1 items. division = size.div number modulo = size % number # create a new array avoiding dup groups = [] start = 0 number.times do |index| length = division + (modulo > 0 && modulo > index ? 1 : 0) groups << last_group = slice(start, length) last_group << fill_with if fill_with != false && modulo > 0 && length == division start += length end if block_given? groups.each(&block) else groups end end