From: Robert Gleeson Date: 2009-12-11T22:49:14+09:00 Subject: Re: Redirecting standard output > 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? > - 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. > Your last solution is better (although you do not necessarily need a > constant for that). Yup. I just choose a constant to illustrate an example - if $stdout was changing often, I'd use something more appropriate. > Btw, apparently you don't even need the mode - > that is inherited from the original. So you can do: > > $ ruby19 -e '$stderr.reopen("log"); $stderr.puts("foo")' ; cat log > foo > $ ruby19 -e '$stdin.reopen("log"); p gets' > "foo\n" > $ Cool, good to know -- Posted via http://www.ruby-forum.com/.