From: Robert Klemme Date: 2009-05-22T13:27:19+09:00 Subject: Re: I want to redirect stderr to StringIO. On 20.05.2009 21:01, Eric Hodel wrote: > On May 19, 2009, at 23:14, Robert Klemme wrote: >> 2009/5/20 Eric Hodel : >>> 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. > > Most of the time people capture IO from sub processes via Kernel#` or > IO::popen instead of handling the sub process themselves. Ruby's nice > features make use of #reopen an exceptional circumstance, but there > are times where it's appropriate. You do not make that point on the blog entry which provoked my remark. (I wanted to place it there but comments are closed.) >> 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 > > This warning says otherwise. Please do not let yourself be distracted by the warning. The crucial point is that after $defout has been reassigned the "2" does not appear any more on the screen => this is the object which is used by #puts. > They happen to be the same object though: > > $ ruby -e 'p $stdout.object_id, $defout.object_id' > 97260 > 97260 Yes - until you reassign any of them. :-) Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/