Class: Pupa::Processor::Yielder

Inherits:
Object
  • Object
show all
Defined in:
lib/pupa/processor/yielder.rb

Overview

A lazy enumerator.

Instance Method Summary collapse

Constructor Details

#initializeYielder

The given block should yield objects to add to the enumerator.



10
11
12
13
14
15
# File 'lib/pupa/processor/yielder.rb', line 10

def initialize
  @fiber = Fiber.new do
    yield
    raise StopIteration
  end
end

Instance Method Details

#eachObject

Yields each object in the enumerator to the given block.



18
19
20
21
22
23
24
25
26
# File 'lib/pupa/processor/yielder.rb', line 18

def each
  if block_given?
    loop do
      yield self.next
    end
  else
    to_enum
  end
end

#nextObject

Returns the next object in the enumerator, and moves the internal position forward. When the position reaches the end, StopIteration is raised.



30
31
32
33
34
35
36
# File 'lib/pupa/processor/yielder.rb', line 30

def next
  if @fiber.alive?
    @fiber.resume
  else
    raise StopIteration
  end
end

#to_enumEnumerator

Returns a lazy enumerator.

Returns:

  • (Enumerator)

    a lazy enumerator



41
42
43
44
45
46
47
# File 'lib/pupa/processor/yielder.rb', line 41

def to_enum
  Enumerator.new do |y|
    loop do
      y << self.next
    end
  end
end