Neil Mc Laughlin
12/2/2003 3:32:00 PM
I had to catch everything written to $stderr in a variable some time ago.
Rather than using pipes, I tweaked the $stderr variable itself, adding the
new functionality.
class << $stderr
alias orig_write write
def write(str)
@playback = "" unless defined? @playback
@playback << str
orig_write(str)
end
def playback
playback = @playback
@playback = ""
clear_playback
return playback
end
def clear_playback
@playback = ""
end
end
Now you can do
irb(main):104:0> $stderr.write("hello")
and
irb(main):105:0> $stderr.playback
"hello"
Hope this is some help.
Neil.
----- Original Message -----
From: "Johannes Barre" <igel@igels.net>
To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
Sent: Tuesday, December 02, 2003 12:55 AM
Subject: buffering of $stdout
> Hi list!
>
> I'm new to ruby, so be nice to me ;)
> I want to capture everything written to the strout and get it in a
> variable. So I did this:
>
> stdout = $stdout
> stdoutin, $stdout = IO.pipe
>
> puts "Hello World"
>
> tmp = $stdout
> $stdout = stdout
> $tmp.close
> variable = stdoutin.readlines.join("\n")
>
> It works, but if I write to much, the pipe seems to be full and the
> script hangs. It will never continue, because I start reading from the
> pipe when everthing is written. My idea is to start two theads, one for
> writing and one for reading, but maybe somebody knows a easier way.
>
> Please help!
>
> Johannes
>
>