From: Brian Candler Date: 2003-04-05T20:59:34+09:00 Subject: Re: reading from $stdout ??? On Sat, Apr 05, 2003 at 06:43:04PM +0900, Meinrad Recheis wrote: > thanks brian, that sounds good. but is very complicated for capturing > the whole output of a large program. > > i would like to connect a pipe to the $stdout (i have no gess how to do > it, but hope that you understand my intention) and in one location of my > main loop (in the display procedure) > i would like to read whatever is on the stream and print it to my > graphical output. Ah, in that case probably what you want is: $defout = File.open("log.txt","w") puts "hello" # goes to log file However, any sub-processes which you spawn which write to stdout will write their output directly to the operating system's stdout, bypassing Ruby altogether. The only ways round that that I know of are: (1) explicitly capture the stdout of the child process, using `` or IO.popen as described before, and write it to $defout yourself (2) when spawning the child process, redirect its stdout to a file, e.g. system("ls >log2.txt") (3) redirect stdout before you start the ruby interpreter itself, e.g. ruby myprog.rb >log.txt Regards, Brian.