Method: Transformer#initialize
- Defined in:
- lib/coroutines/base.rb
#initialize(&block) ⇒ Transformer
:call-seq:
Transformer.new { |yielder| ... } -> trans
Creates a new Transformer coroutine defined by the given block.
The block is called with a “yielder” object as parameter. yielder can be used to retrieve a value from the consumption context by calling its await method (as in Consumer.new), and to yield a value by calling its yield method (as in Enumerator.new).
running_sum = Transformer.new do |y|
result = 0
loop { result += y.await; y.yield result }
end
(1..3) >= running_sum >= [] # => [1, 3, 6]
163 164 165 |
# File 'lib/coroutines/base.rb', line 163 def initialize(&block) @self = block end |