redirecting stderr in irb

M

Martin DeMello

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
 
R

Ryan Davis

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
NameError: undefined local variable or method `blah' for main:Object
from (irb):1
% 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.
 
M

Martin DeMello

"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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

:IRB Bug 1
Class instance method 2
Unicode escaping fun & games 0
shutting irb up 4
what's wrong with this picture? 7
Socket hang in thread 1
irb differs from script 0
irb: Segmentation fault 1

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top