Temporary files in Ruby
Manage temporary files in Ruby using the tempfile
module.
Examples
Write a string to a temporary file:
require 'tempfile'
file = Tempfile.new('prefix')
file.write('hello')
Write binary data:
data = open('kitten.jpg').read
file = Tempfile.new('prefix', binmode: true)
file.write(data)
Enforce a file extension on the temporary file:
file = Tempfile.new(['prefix', '.txt'])
Close the file and delete it:
file = Tempfile.new('prefix')
begin
# do something
ensure
file.close
file.unlink
end