Sha256: f53d9223dd16d95535918c056f84674b4fffbe6c1208e87b6af19a2b52bcdd90

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

module Enumerable
  # Standard in Ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
  def each_with_object(memo, &block)
    return to_enum(:each_with_object, memo) unless block_given?
    each {|obj| block.call(obj, memo)}
    memo
  end unless method_defined? :each_with_object

  def chunk(initial_state = nil, &original_block)
    Enumerator.new do |yielder|
      previous = Backports::Undefined
      accumulate = []
      block = initial_state.nil? ? original_block : Proc.new{|val| original_block.yield(val, initial_state.clone)}
      each do |val|
        case key = block.yield(val)
        when nil, :_separator
          next
        when :_singleton
          yielder.yield previous, accumulate unless accumulate.empty?
          yielder.yield key, [val]
          accumulate = []
          previous = Backports::Undefined
        when previous, Backports::Undefined
          accumulate << val
          previous = key
        else
          yielder.yield previous, accumulate unless accumulate.empty?
          accumulate = [val]
          previous = key
        end
      end
      # what to do in case of a break?
      yielder.yield previous, accumulate unless accumulate.empty?
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
backports-1.10.0 lib/backports/1.9/enumerable.rb