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).out_connect(running_sum).out_connect([]) # => [1, 3, 6]
164 165 166 |
# File 'lib/coroutines/base.rb', line 164 def initialize(&block) @self = block end |