From: Jim Weirich Date: 2003-05-03T08:57:54+09:00 Subject: Re: temporary redirection of stdout On Fri, 2003-05-02 at 15:07, ahoward wrote: > anyhow - i can appreciate your opinion but i'm using it in my scripts ;-) but > then again i *love* the entire > > Resource.aquire do |resource| > ... > end > > paradigm and the way it makes code read - to me it feels somehow more > ruby-ish. I've used the following in the past. Now that 1.8 has StringIO, its even better ... def redirect_output(output) old_output = $defout $defout = output yield ensure $defout = old_output end And then ... require 'stringio' sio = StringIO.new redirect_output(sio) { puts "HI" } p sio.string # => "HI\n" By using $defout, I can redirect to string objects (great for testing!). If I try to use reopen, then I can't redirect to StringIO objects :-( -- -- Jim Weirich jweirich@one.net http://w3.one.net/~jweirich --------------------------------------------------------------------- "Beware of bugs in the above code; I have only proved it correct, not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)