title: How to delete lines from file without create a temporary file in ruby?
body: <p>I need delete line of a file sequentially with ruby, but without success. </p>
  
  <p>If i open the file with w+ flag, it gets completely empty, and if i open with r+ flag, nothing happens.
  I even tried do it with by sed's system command, without results too.
  What's wrong?</p>
  
  <pre><code>$ cat teste.txt 
  foo
  bar
  
  - This is by ruby way:
  $ cat ruby.rb 
  #!/usr/bin/ruby
  
  path = '/home/paulosgf/hack/metasploit/teste.txt'
  File.open(path, 'r+') do |file|
      file.each do |line|
          puts(line)
          line.gsub(/^.$\n/, '')
      end
  file.close()
  end
  
  $ ./ruby.rb 
  foo
  bar
  
  $ cat teste.txt (nothing happens)
  foo
  bar
  
  - This is by sed way:
  $ cat sed.rb 
  #!/usr/bin/ruby
  
  path = '/home/paulosgf/hack/metasploit/teste.txt'
  File.open(path, 'r+') do |file|
      file.each do |line|
          puts(line)
          system("sed -i", "/#{line}/d #{path}")
      end
  file.close()
  end
  
  $ ./sed.rb 
  foo
  bar
  
  $ cat teste.txt 
  foo
  bar
  </code></pre>
  
  <p>I need get the next line, run some processing and delete these line at end, but all lines remains on file yet.</p>