Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →
class method
Tempfile.create_anonymous
create_anonymous(basename="", tmpdir=nil, mode: 0, **options) { |tmpfile| ... }
lib/tempfile.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.4.1/lib/tempfile.rb, line 606
def create_anonymous(basename="", tmpdir=nil, mode: 0, **options, &block)
tmpfile = nil
tmpdir = Dir.tmpdir() if tmpdir.nil?
if defined?(File::TMPFILE) # O_TMPFILE since Linux 3.11
begin
tmpfile = File.open(tmpdir, File::RDWR | File::TMPFILE, 0600)
rescue Errno::EISDIR, Errno::ENOENT, Errno::EOPNOTSUPP
# kernel or the filesystem does not support O_TMPFILE
# fallback to create-and-unlink
end
end
if tmpfile.nil?
mode |= File::SHARE_DELETE | File::BINARY # Windows needs them to unlink the opened file.
tmpfile = create_with_filename(basename, tmpdir, mode: mode, **options)
File.unlink(tmpfile.path)
tmppath = tmpfile.path
end
path = File.join(tmpdir, '')
unless tmppath == path
# clear path.
tmpfile.autoclose = false
tmpfile = File.new(tmpfile.fileno, mode: File::RDWR, path: path)
PathAttr.set_path(tmpfile, path) if defined?(PathAttr)
end
if block
begin
yield tmpfile
ensure
tmpfile.close
end
else
tmpfile
end
end
No documentation available