title: Ruby: overwrite line in file
body: <p>I am trying to log some state to a file using standard file i/o in ruby. The file will just have one line with a number in it. I want to read in the line and then on each iteration of a loop I want to update this number.</p>
  
  <p>I know I can read in the file with </p>
  
  <pre><code>file = File.open('out.log', 'a+')
  num = file.readline
  </code></pre>
  
  <p>The problem is, I don't know how to then overwrite the first line in a loop without just re-opening the file every iteration i.e.</p>
  
  <pre><code>file = File.open('out.log', 'w')
  </code></pre>
  
  <p>which will create an empty file</p>