Sha256: ba62f55d64af8cb70a7ea3b10aefc55ab6854ba67ae75c28e29312a475eda99a

Contents?: true

Size: 1.27 KB

Versions: 11

Compression:

Stored size: 1.27 KB

Contents

unless Enumerable.method_defined? :chain
  module Enumerable
    def chain(*enums)
      Enumerator::Chain.new(self, *enums)
    end
  end

  Enumerator = Enumerable::Enumerator unless Object.const_defined? :Enumerator # For 1.8.x

  class Enumerator::Chain < Enumerator
    # rubocop:disable Lint/MissingSuper
    def initialize(*enums)
      @enums = enums
      @rewindable = -1
      # This self is necessary to pass RubySpec,
      # See rubyspec/core/enumerator/chain/initialize_spec.rb
      # ...it checks what call of #initialize on non-initalized object returns
      self # rubocop:disable Lint/Void
    end
    # rubocop:enable Lint/MissingSuper

    def each(*args, &block)
      @enums.each_with_index do |enum, i|
        @rewindable = i
        enum.each(*args, &block)
      end
    end

    def size
      sum = 0
      @enums.each do |enum|
        s = enum.size
        return s if s == nil || s == Float::INFINITY
        sum += s
      end
      sum
    end

    def inspect
      detail = @enums.map(&:inspect).join(', ')
      "#<Enumerator::Chain: [#{detail}]>"
    end

    def rewind
      @rewindable.downto(0) do |i|
        enum = @enums[i]
        enum.rewind if enum.respond_to? :rewind
      end
      self
    end
  end unless Enumerator.const_defined? :Chain
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
backports-3.25.0 lib/backports/2.6.0/enumerable/chain.rb
backports-3.24.1 lib/backports/2.6.0/enumerable/chain.rb
backports-3.24.0 lib/backports/2.6.0/enumerable/chain.rb
backports-3.23.0 lib/backports/2.6.0/enumerable/chain.rb
backports-3.22.1 lib/backports/2.6.0/enumerable/chain.rb
backports-3.22.0 lib/backports/2.6.0/enumerable/chain.rb
backports-3.21.0 lib/backports/2.6.0/enumerable/chain.rb
backports-3.20.2 lib/backports/2.6.0/enumerable/chain.rb
backports-3.20.1 lib/backports/2.6.0/enumerable/chain.rb
backports-3.20.0 lib/backports/2.6.0/enumerable/chain.rb
backports-3.19.0 lib/backports/2.6.0/enumerable/chain.rb