From: Robert Klemme Date: 2009-12-12T00:32:44+09:00 Subject: Re: Redirecting standard output 2009/12/11 Robert Gleeson : >> The gem was not needed for the redirection functionality but for >> getting at information about the method. > Yup, I completely missed that! I was tired, and misread the thread > completely. > >>> $stdout = File.new '/path/to/file', 'w' >>> puts 'foobar' >>> $stdout = STDOUT >>> puts 'foobar' >> >> This does only work for redirections of output for the particular >> process.  It will fail in these cases >> >> - sub processes started > > Hmm really? I thought a fork() would copy the memory from the parent > process into the subprocess. > Example: > >  $stdout = File.open 'foobar.txt', 'w'; fork { $stdout.puts 'Hello' } > >  $ cat foobar.txt >    Hello >    => 4301 > > Am I misunderstanding you? I probably wasn't specific enough: sub processes which use exec to overlay the image will not inherit redirection if you simply assign to $stdout: 16:24:59 ~$ ruby19 -e '$stdout = File.open("log","w"); puts 123; system("date")' Fri Dec 11 16:25:05 WEST 2009 16:25:05 ~$ cat -n log 1 123 16:25:08 ~$ ruby19 -e '$stdout.reopen("log"); puts 456; system("date")' 16:25:14 ~$ cat -n log 1 456 2 Fri Dec 11 16:25:13 WEST 2009 16:25:15 ~$ >> - all entities which remembered $stdout instance (e.g. a logger) > > I'm not sure I follow you here either. > A reference to $stdout would persist. > >  foo = $stdout >  $stdout = File.new 'foobar.txt', 'w' >  # foo still references $stdout > > Unless they use #dup, I don't see how the reference to $stdout wouldn't > persist. And exactly that is a problem: redirection does not take place for whoever owns foo => it failed. Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/