Sha256: 9b2c9ef5b678923d957e5cb505ae3f64fa3c36f4a271589e272079af5a16f7f3
Contents?: true
Size: 785 Bytes
Versions: 3
Compression:
Stored size: 785 Bytes
Contents
#pretty useful rails method. Splits an array into groups class Array def in_groups(number, fill_with = nil) # size / 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 / 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) padding = fill_with != false && modulo > 0 && length == division ? 1 : 0 groups << slice(start, length).concat([fill_with] * padding) start += length end if block_given? groups.each { |g| yield(g) } else groups end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
mogbak-0.2.1 | lib/monkey_patch.rb |
mogbak-0.2.0 | lib/monkey_patch.rb |
mogbak-0.1.2 | lib/monkey_patch.rb |