Sha256: d609e0f4957497e1eae2395d45d6b33d5de73173c3b73c5923ac87e7dcdcfe9e

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

require 'jinx/helpers/collection'

module Jinx
  # A MultiEnumerator iterates over several Enumerators in sequence. Unlike Array#+,
  # MultiEnumerator reflects changes to the underlying enumerators.
  #
  # @example
  #   a = [1, 2]
  #   b = [4, 5]
  #   ab = MultiEnumerator.new(a, b)
  #   ab.to_a #=> [1, 2, 4, 5]
  #   a << 3; b << 6; ab.to_a #=> [1, 2, 3, 4, 5, 6]
  #   MultiEnumerator.new(ab, [7]).to_a #=> [1, 2, 3, 4, 5, 6, 7]
  class MultiEnumerator
    include Enumerable, Collection

    # Initializes a new {MultiEnumerator} on the given components.
    #
    # @param [<Enumerable>] the component enumerators to compose
    # @yield [item] the optional appender block
    # @yieldparam item the item to append
    def initialize(*enums, &appender)
      super()
      @components = enums
      @components.compact!
      @appender = appender
    end

    # Iterates over each of this MultiEnumerator's enumerators in sequence.
    def each
      @components.each do |enum|
        enum.each { |item| yield item }
      end
    end
    
    # @param item the item to append
    # @raise [NoSuchMethodError] if this {MultiEnumerator} does not have an appender
    def <<(item)
      @appender ? @appender << item : super
    end
      
    # Returns the union of the results of calling the given method symbol on each component.
    def method_missing(symbol, *args)
      self.class.new(@components.map { |enum|enum.send(symbol, *args) })
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jinx-2.1.4 lib/jinx/helpers/multi_enumerator.rb