Sha256: fa2aabe42500a54261ab8b81a433733525003174e7a4070c1533150b04227d89

Contents?: true

Size: 694 Bytes

Versions: 12

Compression:

Stored size: 694 Bytes

Contents

module Array::EachBatch
  def each_batch(batch_size = 1000, &block)
    raise "Invalid parameter #{batch_size.inspect}, must be a Fixnum" unless batch_size.is_a?(Fixnum)

    data = dup
    begin
      batch = data.slice!(0..(batch_size-1))
      yield batch
    end until data.empty?
    self
  end
end

Array.send :include, Array::EachBatch

module Array::Cross::Etest
  def test_each_batch
    r = []
    [1,2,3,4,5,6,7,8,9].each_batch 3 do |batch|
      r << batch
    end 
    
    assert_equal( [[1,2,3],[4,5,6],[7,8,9]], r)

    r = []
    [1,2,3,4,5,6,7].each_batch 3 do |batch|
      r << batch
    end 
    
    assert_equal( [[1,2,3],[4,5,6],[7]], r)
  end
end if VEX_TEST == "base"

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
vex-0.6.2 lib/vex/base/array/each_batch.rb
vex-0.4.4 lib/vex/base/array/each_batch.rb
vex-0.4.2 lib/vex/base/array/each_batch.rb
vex-0.3.3 lib/vex/base/array/each_batch.rb
vex-0.2.9 lib/vex/base/array/each_batch.rb
vex-0.2.8 lib/vex/base/array/each_batch.rb
vex-0.2.7 lib/vex/base/array/each_batch.rb
vex-0.2.6 lib/vex/base/array/each_batch.rb
vex-0.2.5 lib/vex/base/array/each_batch.rb
vex-0.2.2 lib/vex/base/array/each_batch.rb
vex-0.2.1 lib/vex/base/array/each_batch.rb
vex-0.2 lib/vex/base/array/each_batch.rb