Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > redirecting stderr in irb

Reply
Thread Tools

redirecting stderr in irb

 
 
Martin DeMello
Guest
Posts: n/a
 
      12-21-2009
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

 
Reply With Quote
 
 
 
 
Ryan Davis
Guest
Posts: n/a
 
      12-21-2009

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.


 
Reply With Quote
 
 
 
 
Martin DeMello
Guest
Posts: n/a
 
      12-21-2009
On Mon, Dec 21, 2009 at 3:39 PM, Ryan Davis <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Redirecting stdin, stdout, and stderr to a window Michael McGarry Python 1 12-16-2004 11:29 PM
redirecting stderr Michele Simionato Python 2 11-25-2004 01:25 PM
wxPython & redirecting stderr flupke Python 2 08-10-2004 11:56 PM
redirecting stderr and stdout Jon Landenburer Perl 1 05-13-2004 07:38 AM
Redirecting Python stdout ,stderr and stdin Jan Knop Python 4 11-24-2003 10:14 AM



Advertisments