I am trying to make use of the jirb_swing console so that if I
encountered an exception in my code, I can launch jirb_swing for
debugging purpose. Unfortunately, the current jirb_swing hijacks the io
stream and does not return to io back to the original console after the
frame has been disposed. So what I did is I add code to save the
original io stream first and restore it after the frame has been
stdin = JRuby.runtime.global_variables.get("$stdin") stdout = JRuby.runtime.global_variables.get("$stdout") stderr = JRuby.runtime.global_variables.get("$stderr") Readline.module_eval do alias readline_old readline module_function :readline_old end # code start the jirb_swing JRuby.runtime.global_variables.set("$stdin", stdin) JRuby.runtime.global_variables.set("$stdout", stdout) JRuby.runtime.global_variables.set("$stderr", stderr) Readline.module_eval do alias readline readline_old module_function :readline end
This all works fine but I noticed if I used “gets” in my jirb_swing
console, after the jirb_swing frame is disposed, if I call gets again,
it will no longer take any of my inputs. After digging through the
jruby source codes, I noticed the cause of the problem is that “gets”
implementation seems to cache the $stdin value when it is invoked the
first time. Even though I have restored my $stdin back, the gets
function is still using the old $stdin value. I came up with a short
ruby code to prove this theory:
old_stdin=$stdin $stdin=StringIO.new("HelloWorld") puts "type in some thing:"; puts gets; $stdin=old_stdin puts gets
The second gets will return “nil” instead of asking for user input. The
same code seems to work properly on Ruby.
The code is tested on JRuby 1.1.6.
Is this a bug in the jruby implementation? Any possible way I might be
able to work around it?