Sha256: 26cec67e2cf45aa8a13c4fa68f157119fe46f2e1a2ea1f9750378488fd078251

Contents?: true

Size: 769 Bytes

Versions: 52

Compression:

Stored size: 769 Bytes

Contents

class Array
  # copied from ActiveSupport CoreExtensions
  def in_groups(number, fill_with = nil)
    # size / number gives minor group size;
    # size % number gives how many objects need extra accomodation;
    # 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

52 entries across 52 versions & 1 rubygems

Version Path
mspire-0.10.8.0 lib/core_ext/array/in_groups.rb
mspire-0.10.7.3 lib/core_ext/array/in_groups.rb
mspire-0.10.7.2 lib/core_ext/array/in_groups.rb
mspire-0.10.7.1 lib/core_ext/array/in_groups.rb
mspire-0.10.7 lib/core_ext/array/in_groups.rb
mspire-0.10.6 lib/core_ext/array/in_groups.rb
mspire-0.10.5 lib/core_ext/array/in_groups.rb
mspire-0.10.4 lib/core_ext/array/in_groups.rb
mspire-0.10.3 lib/core_ext/array/in_groups.rb
mspire-0.10.2 lib/core_ext/array/in_groups.rb
mspire-0.10.1 lib/core_ext/array/in_groups.rb
mspire-0.10.0 lib/core_ext/array/in_groups.rb
mspire-0.9.2 lib/core_ext/array/in_groups.rb
mspire-0.9.1 lib/core_ext/array/in_groups.rb
mspire-0.9.0 lib/core_ext/array/in_groups.rb
mspire-0.8.7 lib/core_ext/array/in_groups.rb
mspire-0.8.6.2 lib/core_ext/array/in_groups.rb
mspire-0.8.6.1 lib/core_ext/array/in_groups.rb
mspire-0.8.6 lib/core_ext/array/in_groups.rb
mspire-0.8.5 lib/core_ext/array/in_groups.rb