Skip to content

End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

class method

Tempfile.create

create(basename="", tmpdir=nil, mode: 0, **options) { |tmpfile| ... }

Creates a temporary file as a usual File object (not a Tempfile). It does not use finalizer and delegation, which makes it more efficient and reliable.

If no block is given, this is similar to Tempfile.new except creating File instead of Tempfile. In that case, the created file is not removed automatically. You should use File.unlink to remove it.

If a block is given, then a File object will be constructed, and the block is invoked with the object as the argument. The File object will be automatically closed and the temporary file is removed after the block terminates, releasing all resources that the block created. The call returns the value of the block.

In any case, all arguments (basename, tmpdir, mode, and **options) will be treated the same as for Tempfile.new.

Example
Tempfile.create('foo', '/home/temp') do |f|
   # ... do something with f ...
end
See also .new .open also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0