Переназначение глобального $ stdout на консоль - ruby

Я пытаюсь установить $ 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 не сбрасывается на консоль?

Ответы на вопрос(2)

Ваш ответ на вопрос