# Redirecting stderr in irb

**URL:** <https://www.ruby-forum.com/t/redirecting-stderr-in-irb/180525>\
**Category:** Ruby\
**Created:** [December 21, 2009, 9:27am UTC](https://www.ruby-forum.com/t/redirecting-stderr-in-irb/180525 "2009-12-21T09:27:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [December 21, 2009, 9:27am UTC](https://www.ruby-forum.com/t/redirecting-stderr-in-irb/180525/1 "2009-12-21T09:27:17Z")

</div>

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’  
=\> true  
irb(main):002:0\> $stderr = StringIO.new  
=\> #StringIO:0x9b87698  
irb(main):003:0\> hello  
NameError: undefined local variable or method `hello' for main:Object from (irb):3 from /usr/bin/irb:12:in`’  
irb(main):004:0\> $\> = 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

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [December 21, 2009, 11:13am UTC](https://www.ruby-forum.com/t/redirecting-stderr-in-irb/180525/2 "2009-12-21T11:13:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [December 21, 2009, 11:38am UTC](https://www.ruby-forum.com/t/redirecting-stderr-in-irb/180525/3 "2009-12-21T11:38:24Z")

</div>

On Mon, Dec 21, 2009 at 3:39 PM, Ryan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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
