From: Bill Kelly Date: 2003-04-06T01:40:55+09:00 Subject: Re: reading from $stdout ??? Hi Meinrad, From: "Meinrad Recheis" > > so it seems to me, that it is necessary to go the long and slowly way > over a log file. > > i have annother idea: > what do you think about aliasing and overwriting all outputting methods > in IO like puts, write, print and so on? would that affect really all > output? I'm still not quite sure I'm clear on what you're trying to do and why, say, popen() wouldn't do the job for you... But here's another popen variation that sounds possibly similar to what you're describing... (Did you say what Operating System you're on? The following won't work on Windows, I believe, due to lack of availibilty of fork. (Probably works under cygwin though (?)) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #!/usr/local/bin/ruby -w # parent, here, can read everything child sends to stdout def do_parent_stuff(io) io.each {|line| puts "child emitted: #{line}" } end # child gets to do both ruby print statements and run # system commands, all of whose stdout are piped to the parent def do_child_stuff puts "Hello!!! I'm child #{Process.pid}." system("ls -l /usr/lib/ruby/1.6/net") puts "Bye now!!!" end IO.popen("-") do |io| io ? do_parent_stuff(io) : do_child_stuff end ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ on my system the output is: child emitted: Hello!!! I'm child 2743. child emitted: total 172 child emitted: -rw-r--r-- 1 root root 13968 Jul 24 2002 ftp.rb child emitted: -rw-r--r-- 1 root root 32781 Jul 24 2002 http.rb child emitted: -rw-r--r-- 1 root root 52629 Jul 24 2002 imap.rb child emitted: -rw-r--r-- 1 root root 14189 Jul 24 2002 pop.rb child emitted: -rw-r--r-- 1 root root 14652 Jul 24 2002 protocol.rb child emitted: -rw-r--r-- 1 root root 11220 Jul 24 2002 smtp.rb child emitted: -rw-r--r-- 1 root root 19823 Jul 24 2002 telnet.rb child emitted: Bye now!!! Hope this helps, Bill