From: 7stud -- Date: 2009-02-09T21:26:29+09:00 Subject: Re: assignment to $stdout deprecated? Brian Candler wrote: > Thanks for the detailed response. > If you reassign $stdout to point to a completely different Ruby object, > then any code you write which does $stdout.puts will write to this > object - but FD 1 still remains connected to the original stdout. > Ok. > Therefore, STDOUT.puts will still write to the original destination, as > will any child process. > Ok. > $stdout = File.open("/tmp/stdout.txt","w") > > puts "Hello" # uses $stdout, goes to the file > STDOUT.puts "World" # goes to the terminal (FD 1) > system("echo Wheee") # goes to the terminal (FD 1) > AFAIK, all child processes receive a copy of their environment including copies of all the variables in the parent. So echo doesn't write to $stdout because echo doesn't understand ruby and therefore ignores variables like $stdout? > If you need to redirect to a StringIO object, then you have little > choice but to use $stdout, because a StringIO is not a Unix file, i.e. > it doesn't have an entry in the file descriptor table. > Ok. > (If you wanted to get very fancy, you could perhaps set up a pipe, > connect stdout to the writer end, and have a Ruby thread reading from > the reader end and appending to a StringIO object. But you'd only jump > through those hoops if you wanted the stdout from spawned child > processes to write to the StringIO too, and in that case you'd be better > off using IO.popen anyway) Thanks for taking the time to write such an informative post. -- Posted via http://www.ruby-forum.com/.