Thursday, March 13, 2008

Redirect STDOUT in Ruby

若想对标准输入、输出、错误输出等进行重定向(redirect)时,可以使用IO#reopen:
STDOUT.reopen("/tmp/foo", "a+")
不建议使用: $stdout = File.open("/tmp/foo", "a+"), 这还有个输出缓存的过程,需要用IO#sync(IO#fsync)或者是IO#flush才会将输入写入log中。


Reference:
http://www.ruby-lang.org.cn/forums/archiver/tid-39.html

No comments :