Class: Enumerator::Chain

Inherits:
Enumerator show all
Defined in:
lib/backports/2.6.0/enumerable/chain.rb

Instance Method Summary collapse

Methods inherited from Enumerator

#initialize_with_optional_block, produce, product

Constructor Details

#initialize(*enums) ⇒ Chain

Returns a new instance of Chain.



12
13
14
15
16
17
18
19
# File 'lib/backports/2.6.0/enumerable/chain.rb', line 12

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

Instance Method Details

#each(*args, &block) ⇒ Object



22
23
24
25
26
27
# File 'lib/backports/2.6.0/enumerable/chain.rb', line 22

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

#inspectObject



39
40
41
42
# File 'lib/backports/2.6.0/enumerable/chain.rb', line 39

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

#rewindObject



44
45
46
47
48
49
50
# File 'lib/backports/2.6.0/enumerable/chain.rb', line 44

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

#sizeObject



29
30
31
32
33
34
35
36
37
# File 'lib/backports/2.6.0/enumerable/chain.rb', line 29

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