【发布时间】:2012-03-04 12:14:41
【问题描述】:
我正在尝试将 $stdout 设置为临时写入文件,然后再返回文件。
test.rb :
old_stdout = $stdout
$stdout.reopen("mytestfile.out",'w+')
puts "this goes in mytestfile"
$stdout= old_stdout
puts "this should be on the console"
$stdout.reopen("mytestfile1.out",'w+')
puts "this goes in mytestfile1:"
$stdout = old_stdout
puts "this should be back on the console"
这是输出。
ruby test.rb => no output on the console
cat mytestfile.out
this goes in mytestfile
this should be on the console
cat mytestfile1.out
this goes in mytestfile1:
this should be back on the console
我不确定为什么 $stdout 没有重置到控制台?
【问题讨论】:
-
如果您接受其中一个已发布的答案,如果这当然解决了您的问题,我相信有人会很感激!