redirecting stderr in irb

Discussion in 'Ruby' started by Martin DeMello, Dec 21, 2009.

  1. Redirecting $stderr in irb doesn't work but redirecting
    $DEFAULT_OUTPUT does. Can anyone explain why this happens? Also, is
    there any clever way to run irb in a thread and then redirect
    $DEFAULT_OUTPUT only for that thread?

    $ irb
    irb(main):001:0> require 'stringio'
    =3D> true
    irb(main):002:0> $stderr =3D StringIO.new
    =3D> #<StringIO:0x9b87698>
    irb(main):003:0> hello
    NameError: undefined local variable or method `hello' for main:Object
    =A0 =A0 =A0 =A0from (irb):3
    =A0 =A0 =A0 =A0from /usr/bin/irb:12:in `<main>'
    irb(main):004:0> $> =3D StringIO.new
    irb(main):005:0> hello
    irb(main):006:0>

    (Sorry if you're seeing this twice, didn't get any response in
    ruby-core so I'm forwarding to ruby-talk)

    martin
     
    Martin DeMello, Dec 21, 2009
    #1
    1. Advertising

  2. Martin DeMello

    Ryan Davis Guest

    On Dec 21, 2009, at 00:26 , Martin DeMello wrote:

    > Redirecting $stderr in irb doesn't work but redirecting
    > $DEFAULT_OUTPUT does.


    "doesn't work" is hardly true. You're certainly redirecting $stderr at =
    the time. What you're confusing is that $DEFAULT_OUTPUT is not an alias =
    for $stderr, but for $stdout. This is easily testable just by playing =
    with the shell:

    > % irb --noreadline 2> x
    > >> blah

    > NameError: undefined local variable or method `blah' for main:Object
    > from (irb):1
    > >> ^d

    > % cat x
    > %


    Unfortunately, irb decided to print via STDOUT (which I prefer to keep =
    hands off and play with $stdout only), so messing with it (for me) is a =
    no-no.

    > class StdioOutputMethod<OutputMethod
    > def print(*opts)
    > STDOUT.print(*opts)
    > end
    > end


    I think what you really want to do is to temporarily reassign =
    output_method in the context.
     
    Ryan Davis, Dec 21, 2009
    #2
    1. Advertising

  3. On Mon, Dec 21, 2009 at 3:39 PM, Ryan Davis <> wrote:
    >
    > On Dec 21, 2009, at 00:26 , Martin DeMello wrote:
    >
    >> Redirecting $stderr in irb doesn't work but redirecting
    >> $DEFAULT_OUTPUT does.

    >
    > "doesn't work" is hardly true. You're certainly redirecting $stderr at the time. What you're confusing is that $DEFAULT_OUTPUT is not an alias for $stderr, but for $stdout. This is easily testable just by playing with the shell:


    Yeah, i know that, but irb seems to be printing its errors to $DEFAULT_OUTPUT

    > I think what you really want to do is to temporarily reassign output_method in the context.


    Well, irb.rb has

    def output_value
    if @context.inspect?
    printf @context.return_format, @context.last_value.inspect
    else
    printf @context.return_format, @context.last_value
    end
    end

    So, if I redefine output_method I also need to reopen Irb to override
    output_value and have it call @context.output_method.printf rather
    than printf. Anyway I did that, and regular output gets redirected
    appropriately, but errors don't. It looks like it's hardcoding
    StdioOutputMethod somewhere, but I couldn't figure out how to override
    that.

    martin
     
    Martin DeMello, Dec 21, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon Landenburer

    redirecting stderr and stdout

    Jon Landenburer, May 12, 2004, in forum: Perl
    Replies:
    1
    Views:
    11,268
    Joe Smith
    May 13, 2004
  2. Jan Knop
    Replies:
    4
    Views:
    1,116
    Jan knob
    Nov 24, 2003
  3. flupke

    wxPython & redirecting stderr

    flupke, Aug 10, 2004, in forum: Python
    Replies:
    2
    Views:
    352
    flupke
    Aug 11, 2004
  4. Michele Simionato

    redirecting stderr

    Michele Simionato, Nov 25, 2004, in forum: Python
    Replies:
    2
    Views:
    309
    Peter Otten
    Nov 25, 2004
  5. Michael McGarry
    Replies:
    1
    Views:
    646
    Michael Fuhr
    Dec 16, 2004
Loading...

Share This Page