我试图设置$ 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没有重置为控制台?
我确定有人会喜欢,如果你接受张贴的答案之一,如果这解决了你的问题当然! – user2398029 2012-02-21 02:28:20