我試圖設置$ 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