Project

General

Profile

Feature #5707

temporary file creation without finalizer and delegate.

Added by akr (Akira Tanaka) almost 9 years ago. Updated over 7 years ago.

Status:
Closed
Priority:
Normal
Target version:
[ruby-core:41478]

Description

Tempfile always uses finalizer and delegate.
Sometimes we don't need them.

For example, following test creates a temporary file
using Tempfile.open but the file is used only in the block.
The file is removed when the Tempfile object is collected but
it is possible to remove just after the block return.

test/ruby/test_io.rb:
def test_fcntl_dupfd
Tempfile.open(self.class.name) do |f|
fd = f.fcntl(Fcntl::F_DUPFD, 63)
begin
assert_operator(fd, :>=, 63)
ensure
IO.for_fd(fd).close
end
end
end

So I propose a method, Tempfile.open2, to create temporary file without finalizer and delegate.

The arguments of Tempfile.open2 is same as Tempfile.open.
The return value is File object instead of Tempfile object.

If a block is given, the temporary file is removed after the block returns.

If a block is not given, the temporary file is not removed.
It should be removed using File.unlink.
Non-block form can be used to create non-temporary file.
If you need to change temporary file to non-temporary file, File.rename can be used.

I made a patch to implement Tempfile.open2.
I also made a patch to change tests to use Tempfile.open2 to show usage of it.

A problem of this proposal is the method name.
Tempfile.open2 is not a good name.
Better idea is welcome.


Files

tempfile-open2.patch (1.52 KB) tempfile-open2.patch akr (Akira Tanaka), 12/04/2011 11:31 PM
tempfile-open2-tests.patch (3.16 KB) tempfile-open2-tests.patch akr (Akira Tanaka), 12/04/2011 11:31 PM

Subtasks

Feature #7767: Tempfileで自動的にファイルを削除するClosedakr (Akira Tanaka)02/01/2013Actions

Also available in: Atom PDF