Skip to content
class method

Tempfile.create

(?::String | [ ::String, ::String ] basename, ?::String? tmpdir, ?mode: ::Integer, ?anonymous: bool, **untyped) -> ::File
[A] (?::String | [ ::String, ::String ] basename, ?::String? tmpdir, ?mode: ::Integer, ?anonymous: bool, **untyped) { (::File) -> A } -> A
create(basename="", tmpdir=nil, mode: 0, anonymous: false, **options, &block)

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:

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:

    Example
    Tempfile.create('foo') # => #<File:/tmp/foo20220505-9795-1gok8l9>
    
  • An array of two strings [prefix, suffix]: the generated filename begins with prefix and ends with suffix:

    Example
    Tempfile.create(%w/foo .jpg/) # => #<File:/tmp/foo20220505-17839-tnjchh.jpg>
    

With arguments basename and tmpdir, the file is created in the directory tmpdir:

Example
Tempfile.create('foo', '.') # => #<File:./foo20220505-9795-1emu6g8>

Keyword arguments mode and options are passed directly to the method File.open:

  • The value given for mode must be an integer and may be expressed as the logical OR of constants defined in File::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=true without a block: the file is removed before returning.

  • anonymous=true with 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:

Example
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.

See also .new .open also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0