Class: Enumerator.self::Chain

Inherits:
Enumerator
  • Object
show all
Defined in:
opal/opal/corelib/enumerator/chain.rb

Instance Method Summary collapse

Constructor Details

#initialize(*enums) ⇒ Chain

Returns a new instance of Chain.



6
7
8
9
10
11
12
# File 'opal/opal/corelib/enumerator/chain.rb', line 6

def initialize(*enums)
  `$deny_frozen_access(self)`

  @enums = enums
  @iterated = []
  @object = self
end

Instance Method Details

#each(*args, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'opal/opal/corelib/enumerator/chain.rb', line 14

def each(*args, &block)
  return to_enum(:each, *args) { size } unless block_given?

  @enums.each do |enum|
    @iterated << enum
    enum.each(*args, &block)
  end

  self
end

#inspectObject



43
44
45
# File 'opal/opal/corelib/enumerator/chain.rb', line 43

def inspect
  "#<Enumerator::Chain: #{@enums.inspect}>"
end

#rewindObject



35
36
37
38
39
40
41
# File 'opal/opal/corelib/enumerator/chain.rb', line 35

def rewind
  @iterated.reverse_each do |enum|
    enum.rewind if enum.respond_to? :rewind
  end
  @iterated = []
  self
end

#size(*args) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'opal/opal/corelib/enumerator/chain.rb', line 25

def size(*args)
  accum = 0
  @enums.each do |enum|
    size = enum.size(*args)
    return size if [nil, ::Float::INFINITY].include? size
    accum += size
  end
  accum
end