$ 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がコンソールにリセットされない理由はわかりませんか?
投稿した回答の1つを受け入れると、誰かが喜んでくれたと思います。 – user2398029