Class: Pupa::Processor::Yielder
- Inherits:
-
Object
- Object
- Pupa::Processor::Yielder
- Defined in:
- lib/pupa/processor/yielder.rb
Overview
A lazy enumerator.
Instance Method Summary collapse
-
#each ⇒ Object
Yields each object in the enumerator to the given block.
-
#initialize ⇒ Yielder
constructor
The given block should yield objects to add to the enumerator.
-
#next ⇒ Object
Returns the next object in the enumerator, and moves the internal position forward.
-
#to_enum ⇒ Enumerator
Returns a lazy enumerator.
Constructor Details
#initialize ⇒ Yielder
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
#each ⇒ Object
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 |
#next ⇒ Object
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_enum ⇒ Enumerator
Returns 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 |