From: Massimiliano Mirra Date: 2002-07-09T00:14:33+09:00 Subject: Re: Redirecting standard error On Mon, Jul 08, 2002 at 10:04:04PM +0900, Gilles Filippini wrote: > I saw many examples about how to redirect standard output to - say - a > string buffer. > What about standard error? Has anybody already done this? I wrote and currently use a StringIO. You can do something like this: out, err = StringIO.new, StringIO.new redir(out, err) do puts "hello, world!" $stderr.puts "ooops!" end p out.to_s >> "hello, world!\n" p err.to_s >> "ooops!\n" I use it mostly for writing test cases of things that would otherwise hold conversation with the user. Just assign a StringIO to $stdin, in fact, and you can simulate user input as well. It's far from perfect, biggest deficiency being that it always works in `w+' (read/write) mode, so it doesn't prevent from doing stupid things. It will become unnecessary when Ruby 1.8 will be out as it contains a StringIO of its own. If you want to have a look at it, I'll put it online at http://www.allruby.com/rpkg/stringio.rb by tonight. Massimiliano