Reuse a Fiber in Ruby
I use EventMachine.open_keyboard in my irc client app that uses rbcurse (ncurses gem), as follows:
Fiber.new do EM.open_keyboard(NbKeyboard) do |kb| $input = Readline.readline("") $buffer << $input end end.resume
This code has to be in a Fiber and catch input in a nonblocking way. After I try to type a second line, ruby throws a "dead fiber" exception. I tried adding loops, but that blocks as well, and changing the fiber into a thread doesn't work with the keyboard module. Other ways I tried block all my threads, one of which is responsible for keeping the buffer updated/refreshed. How can I change the code to keep the Fiber open/alive after typing the first line? To view the keyboard module (didn't write it myself), go here.
Edit: Thanks to the answer of Sawa I figured it out:
Fiber.new do EM.open_keyboard(NbKeyboard) do |kb| loop do $input = Readline.readline("") $buffer << $input Fiber.yield end end end.resume
at the end of the fiber body.