title: Ensure to close files in Ruby, with rescue and ensure
body: <p>I have the following code:</p>
  
  <pre><code>begin
     @output_file = File.open("output.txt", "w")
     File.read(@input_file).each_line do |line|
     taxify_line(line)
  end
  rescue =&gt; e
     p "Smz went wrong..."
  end
  @output_file.write("Last line of output")
  @output_file.close unless @output_file.nil?
  </code></pre>
  
  <p>What is the correct way of ensuring that regardless of I catch an exception, both files (@input_file and @output_file) are closed?</p>