Name

Tempfile — Temporary file class

Synopsis

Temporary files are always deleted when garbage collection is activated, and Ruby terminates.

Required Library

require ‘tempfile’

Example

require 'tempfile'
f = Tempfile.new("foo")
f.print("foo\n")
f.close
f.open
p f.gets      # => "foo\n"
f.close(true) # f will be automatically removed

Class Method

Tempfile::new(basename[, tmpdir="/tmp"])

Opens a temporary file that includes basename as part of the filename in w+ mode.

Instance Methods

t.open

Reopens the temporary file, allowing its contents to be read from the beginning of the file.

t.close([permanently=false])

Closes the temporary file. If permanently is true, the file is also deleted.

t.path

Returns the path of the temporary file.

In addition to the previous methods, objects of class Tempfile also possess all instance methods of class File.

Get Ruby in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.