class Array
Grabbed from ActiveSupport activesupport/lib/active_support/core_ext/array/grouping.rb, line 60
Public Instance Methods
in_groups(number, fill_with = nil) { |g| ... }
click to toggle source
# File lib/rspec/parts/file_list.rb, line 6 def in_groups(number, fill_with = nil) # 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 { |g| yield(g) } else groups end end