Sha256: e60280a975d6d74ed0216ad299cbd8ec6211842a9a175f653acd42041184b073

Contents?: true

Size: 977 Bytes

Versions: 10

Compression:

Stored size: 977 Bytes

Contents

##
# chain.rb Enumerator::Chain class
# See Copyright Notice in mruby.h

module Enumerable
  def chain(*args)
    Enumerator::Chain.new(self, *args)
  end
end

class Enumerator
  def +(other)
    Chain.new(self, other)
  end

  class Chain
    include Enumerable

    def initialize(*args)
      @enums = args.freeze
      @pos = -1
    end

    def each(&block)
      return to_enum unless block

      i = 0
      while i < @enums.size
        @pos = i
        @enums[i].each(&block)
        i += 1
      end

      self
    end

    def size
      @enums.reduce(0) do |a, e|
        return nil unless e.respond_to?(:size)
        a + e.size
      end
    end

    def rewind
      while 0 <= @pos && @pos < @enums.size
        e = @enums[@pos]
        e.rewind if e.respond_to?(:rewind)
        @pos -= 1
      end

      self
    end

    def +(other)
      self.class.new(self, other)
    end

    def inspect
      "#<#{self.class}: #{@enums.inspect}>"
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
script_core-0.3.2 ext/enterprise_script_service/mruby/mrbgems/mruby-enum-chain/mrblib/chain.rb
script_core-0.3.0 ext/enterprise_script_service/mruby/mrbgems/mruby-enum-chain/mrblib/chain.rb
script_core-0.2.7 ext/enterprise_script_service/mruby/mrbgems/mruby-enum-chain/mrblib/chain.rb
script_core-0.2.6 ext/enterprise_script_service/mruby/mrbgems/mruby-enum-chain/mrblib/chain.rb
script_core-0.2.5 ext/enterprise_script_service/mruby/mrbgems/mruby-enum-chain/mrblib/chain.rb
script_core-0.2.4 ext/enterprise_script_service/mruby/mrbgems/mruby-enum-chain/mrblib/chain.rb
script_core-0.2.3 ext/enterprise_script_service/mruby/mrbgems/mruby-enum-chain/mrblib/chain.rb
script_core-0.2.2 ext/enterprise_script_service/mruby/mrbgems/mruby-enum-chain/mrblib/chain.rb
script_core-0.2.1 ext/enterprise_script_service/mruby/mrbgems/mruby-enum-chain/mrblib/chain.rb
script_core-0.2.0 ext/enterprise_script_service/mruby/mrbgems/mruby-enum-chain/mrblib/chain.rb