From: Brian Candler Date: 2009-12-14T18:58:01+09:00 Subject: Re: Redirecting standard output Robert Gleeson wrote: > I don't see how reassignment or #reopen would make a difference. They > reference the same object after all - for this particular case, their > behavior is the same. > > So what gives? When you assign to $stdout, you are only changing the Ruby global variable called '$stdout' As it happens, Kernel#puts is just a shorthand for $stdout.puts, so it will do what you expect. But the *real* stdout of the process (that is, file descriptor 1 in the process' FD table) is untouched. You can still get at it using the STDOUT constant, or IO.new(1). When you do $stdout.reopen(f), you are actually re-opening FD 1 with a different file - underneath, ruby is doing a dup2() call to copy f.fileno to FD 1. This means that the process' actual stdout has changed. This is what gets inherited by children you fork off. So: $ ruby -e '$stdout = File.open("/dev/null","w"); system("echo hello")' hello $ ruby -e '$stdout.reopen(File.open("/dev/null","w")); system("echo hello")' $ As for deprecation: I have a suspicion it's $defout not $stdout which is deprecated. -- Posted via http://www.ruby-forum.com/.