Sha256: 8d08e13d0eb06a4bd20de769985c1897fc66f92110a8345effaa84bd5921c614

Contents?: true

Size: 721 Bytes

Versions: 7

Compression:

Stored size: 721 Bytes

Contents

require 'enumerator'

module Enumerable

  # Iterate through slices. If slice +steps+ is not
  # given, the arity of the block is used.
  #
  #   x = []
  #   [1,2,3,4].each_by{ |a,b| x << [a,b] }
  #   x  #=> [ [1,2], [3,4] ]
  #
  #   x = []
  #   [1,2,3,4,5,6].each_by(3){ |a| x << a }
  #   x  #=> [ [1,2,3], [4,5,6] ]
  #
  # This is just like each_slice, except that it will check
  # the arity of the block. If each_slice ever suppots this
  # this method can be deprecated.
  #
  #  CREDIT: Trans

  def each_by(steps=nil, &block)
    if steps
      each_slice(steps, &block)
    else
      steps = block.arity.abs
      each_slice(steps, &block)
      #each_slice(steps) {|i| block.call(*i)}
    end
  end

end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
facets-2.4.0 lib/facets/enumerable/each_by.rb
facets-2.4.1 lib/facets/enumerable/each_by.rb
facets-2.4.2 lib/core/facets/enumerable/each_by.rb
facets-2.4.3 lib/core/facets/enumerable/each_by.rb
facets-2.4.4 lib/core/facets/enumerable/each_by.rb
facets-2.4.5 lib/core/facets/enumerable/each_by.rb
mack-facets-0.8.2 lib/gems/facets-2.4.5/lib/core/facets/enumerable/each_by.rb