Answers, in order:
- Fibers are wrong for this. Fibers are a very high level feature meant for cooperative concurrency. Using them for a simple use case covered by functions and blocks doesn't make sense.
No. Look at your REPL, written using only methods:
def repl(output, &backend) input = nil while output != "stop" puts output print ': ' input = gets.chomp output = backend.call(input) end end repl("Enter a string: ") {|input| input.reverse}
Which outputs:
Enter a string: : Hi iH : Hello olleH : Bork kroB
Works perfectly.
Ruby's own documentation: http://ruby-doc.org/core-2.0.0/Fiber.html
When you call
transfer
orresume
multiple times, the arguments are only passed to the block of the fiber the first time. So each time you calltransfer
the backed fiber is still reversing the first string it was passed, because it never got another. That's why fibers are wrong for implementing this.