Skip to main content

tempfile_auto_cleanup

🗑️ Harness Automatic Cleanup with Tempfile.create

Using Tempfile.create with a block ensures that temporary files are automatically closed and unlinked when the block exits, preventing orphaned files and resource leaks. This approach is ideal for short-lived data processing tasks where you don’t need to manage file lifecycle manually.

require 'tempfile'

Tempfile.create(['report', '.txt']) do |file|
file.write("User report data...\n")
file.rewind
puts file.read
end
# file is automatically closed and deleted here