buffering of $stdout

J

Johannes Barre

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
 
N

Neil Mc Laughlin

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" <[email protected]>
To: "ruby-talk ML" <[email protected]>
Sent: Tuesday, December 02, 2003 12:55 AM
Subject: buffering of $stdout
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top