title: Problem redirecting stdout in Ruby script
body: <p>I have the following test Ruby script:</p>
  
  <pre><code>require 'tempfile'
  
  tempfile = Tempfile.new 'test'
  $stderr.reopen tempfile
  $stdout.reopen tempfile
  
  puts 'test stdout'
  warn 'test stderr'
  `mail -s 'test' my@email.com &lt; #{tempfile.path}`
  
  tempfile.close
  tempfile.unlink
  $stderr.reopen STDERR
  $stdout.reopen STDOUT
  </code></pre>
  
  <p>The email that I get has the contents:</p>
  
  <pre><code>test stderr
  </code></pre>
  
  <p>Why is stderr redirecting properly but not stdout?</p>
  
  <p><strong>Edit:</strong> In response to a comment I added a <code>$stdout.flush</code> after the <code>puts</code> line and it printed correctly. So I'll restate my question: what was happening and why does the flush fix it?</p>