title: Why does file.truncate convert my file to 0's in ruby?
body: <p>I have this code:</p>
  
  <pre><code>File.open(name, 'r+') do |f|
        new_file = f.read.sub /ApplicationController/, 'AdminController'
        f.truncate 0
        f.write new_file
        f.close
      end
  </code></pre>
  
  <p>and it's supposed to replace ApplicationController with AdminController, then truncate the file, then write the new contents, and then close it.</p>
  
  <p>However, when it truncates the file, and then writes to it, it looks like this:</p>
  
  <pre><code>0000 0000 0000 0000 0ef3
  </code></pre>
  
  <p>etc...</p>
  
  <p>So truncate is converting the file to hexadecimal. I need it in UTF-8. How can I ensure that the file is UTF-8 before I write to it?</p>