Module: Array::EachBatch

Defined in:
lib/vex/base/array/each_batch.rb

Instance Method Summary collapse

Instance Method Details

#each_batch(batch_size = 1000, &block) ⇒ Object



2
3
4
5
6
7
8
9
10
11
# File 'lib/vex/base/array/each_batch.rb', line 2

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