Forum: JRuby gets seems cached the $stdin value

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
768d87a25403cb77f2d5efc4a14f1fa5?d=identicon&s=25 Gary Lin (Guest)
on 2009-03-12 02:10
(Received via mailing list)
Hi,

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
disposed:

    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?

Thanks,

--Gary
Ede2aa10c6462f1d825143879be59e38?d=identicon&s=25 Charles Oliver Nutter (Guest)
on 2009-03-12 17:48
(Received via mailing list)
Gary Lin wrote:
> 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:
...
> Is this a bug in the jruby implementation?  Any possible way I might be
> able to work around it?

It definitely does sound like a bug, and you should file it with all
information you can provide. As for a workaround, I have no suggestions;
you can always check out from trunk and modify the code yourself, and of
course once we have a fix it will be on trunk and available.

- Charlie

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email
This topic is locked and can not be replied to.