Tempfile.create
# File tmp/rubies/ruby-4.0.0/lib/tempfile.rb, line 558
def Tempfile.create(basename="", tmpdir=nil, mode: 0, anonymous: false, **options, &block)
if anonymous
create_anonymous(basename, tmpdir, mode: mode, **options, &block)
else
create_with_filename(basename, tmpdir, mode: mode, **options, &block)
end
end
Creates a file in the underlying file system; returns a new File object based on that file.
With no block given and no arguments, creates and returns file whose:
-
Class is
File(not Tempfile). -
Directory is the system temporary directory (system-dependent).
-
Generated filename is unique in that directory.
-
Permissions are
0600; see File Permissions. -
Mode is
'w+'(read/write mode, positioned at the end).
The temporary file removal depends on the keyword argument anonymous and whether a block is given or not. See the description about the anonymous keyword argument later.
Example:
f = Tempfile.create # => #<File:/tmp/20220505-9795-17ky6f6> f.class # => File f.path # => "/tmp/20220505-9795-17ky6f6" f.stat.mode.to_s(8) # => "100600" f.close File.exist?(f.path) # => true File.unlink(f.path) File.exist?(f.path) # => false Tempfile.create {|f| f.puts "foo" f.rewind f.read # => "foo\n" f.path # => "/tmp/20240524-380207-oma0ny" File.exist?(f.path) # => true } # The file is removed at block exit. f = Tempfile.create(anonymous: true) # The file is already removed because anonymous f.path # => "/tmp/" (no filename since no file) f.puts "foo" f.rewind f.read # => "foo\n" f.close Tempfile.create(anonymous: true) {|f| # The file is already removed because anonymous f.path # => "/tmp/" (no filename since no file) f.puts "foo" f.rewind f.read # => "foo\n" }
The argument basename, if given, may be one of the following:
-
A string: the generated filename begins with
basename:ExampleTempfile.create('foo') # => #<File:/tmp/foo20220505-9795-1gok8l9>
-
An array of two strings
[prefix, suffix]: the generated filename begins withprefixand ends withsuffix:ExampleTempfile.create(%w/foo .jpg/) # => #<File:/tmp/foo20220505-17839-tnjchh.jpg>
With arguments basename and tmpdir, the file is created in the directory tmpdir:
Tempfile.create('foo', '.') # => #<File:./foo20220505-9795-1emu6g8>
Keyword arguments mode and options are passed directly to the method File.open:
-
The value given for
modemust be an integer and may be expressed as the logical OR of constants defined inFile::Constants. -
For
options, see Open Options.
The keyword argument anonymous specifies when the file is removed.
-
anonymous=false(default) without a block: the file is not removed. -
anonymous=false(default) with a block: the file is removed after the block exits. -
anonymous=truewithout a block: the file is removed before returning. -
anonymous=truewith a block: the file is removed before the block is called.
In the first case (anonymous=false without a block), the file is not removed automatically. It should be explicitly closed. It can be used to rename to the desired filename. If the file is not needed, it should be explicitly removed.
The File#path method of the created file object returns the temporary directory with a trailing slash when anonymous is true.
When a block is given, it creates the file as described above, passes it to the block, and returns the block’s value. Before the returning, the file object is closed and the underlying file is removed:
Tempfile.create {|file| file.path } # => "/tmp/20220505-9795-rkists"
Implementation note:
The keyword argument anonymous=true is implemented using FILE_SHARE_DELETE on Windows. O_TMPFILE is used on Linux.
Related: Tempfile.new.