title: Edit each line in a file in Ruby
body: <p>I'm trying to find a simple way of editing each line in a file, and I'm having some trouble understanding how to use the <code>File</code> class to do so.</p>
  
  <p>The file I want to edit has several hundred lines with comma separated values in each line. I'm only interested in the first value in each line, and I want to delete all values after the first one. I tried to do the following:</p>
  
  <pre><code>File.open('filename.txt', 'r+') do |file|
    file.each_line { |line| line = line.split(",")[0] }
    file.write
    file.close
  end
  </code></pre>
  
  <p>Which doesn't work because <code>File.write</code> method requires the contents to be written as an argument.</p>
  
  <p>Could someone enlighten me as to how I could achieve the desired effect?</p>