Massimiliano Mirra wrote:
> 
> 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"
> 

Unfortunately it doesn't work as I expected. I tried this:

maison:~/test$ irb
irb(main):001:0> require "StringIO"
true
irb(main):002:0> out = StringIO.new
#<StringIO:0x402a3c64 @buffer="", @pos=0>
irb(main):003:0> err = StringIO.new
#<StringIO:0x402a0780 @buffer="", @pos=0>
irb(main):004:0> redir(out, err) do
irb(main):005:1* `ls dsfgdsfgsdgsdg`
irb(main):006:1> end
ls: dsfgdsfgsdgsdg: No such file or directory <= shouldn't have been
                                                 printed here
""
irb(main):007:0> out.to_s
""
irb(main):008:0> err.to_s
""                                            <= but here
irb(main):009:0>


_gilles.