From: Robert Klemme Date: 2009-05-20T15:14:22+09:00 Subject: Re: I want to redirect stderr to StringIO. 2009/5/20 Eric Hodel : > On May 19, 2009, at 21:30, ErMaker wrote: > >>>> IO.new(2).reopen(StringIO.new) >> >> TypeError: can't convert StringIO into String >>       from (irb):1:in `reopen' >>       from (irb):1 >>>> >>>> IO.new(2).reopen(IO.new(1)) >> >> => # >> >> StringIO also do as IO, but IO#reopen fails. >> >> I want to redirect stderr to StringIO, but it doesn't work. > > Always use reopen as a last resort. > > http://blog.segment7.net/articles/2006/08/17/stdout-vs-stdout I am not sure I agree. Actually, if you want the redirection to be permanent for sub processes you have to use $stdout.reopen. And this is not a "last resort" but the proper solution. Btw, in 1.8 there is also $defout - I believe _that- is the stream used by Kernel#puts and the like in those versions: 08:13:00 ~$ ruby -r stringio -e 'puts 1; $defout=StringIO.new; puts 2' 1 -e:1: warning: $defout is obsolete; use $stdout instead 08:13:30 ~$ Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/