From: "Glass_saga (Masaki Matsushita)" <glass.saga@...>
Date: 2012-10-16T09:49:25+09:00
Subject: [ruby-core:48015] [ruby-trunk - Feature #7148] Improved Tempfile w/o DelegateClass


Issue #7148 has been updated by Glass_saga (Masaki Matsushita).


Hello,

Yukihiro Matsumoto wrote:
> I'd expect it to copy the underlying temporary file.

Is the behavior of #dup you expect like the following?

def dup
  dupe = self.class.new(@basename)
  IO.copy_stream(self, dupe, 0)
  dupe
end

I think the reason why Tempfile uses DelegateClass is that to implement Tempfile#open without it used to be difficult.
To implement it as subclass of File, self must be reopened with full configuration, mode and opts.
IO#reopen used not to accept them, but now it accepts after r37071.
----------------------------------------
Feature #7148: Improved Tempfile w/o DelegateClass
https://bugs.ruby-lang.org/issues/7148#change-30801

Author: Glass_saga (Masaki Matsushita)
Status: Open
Priority: Normal
Assignee: 
Category: lib
Target version: 


I propose improved Tempfile without DelegateClass().
Present Tempfile has following problems.

1) confusing inspect

t = Tempfile.new("foo") #=> #<File:/tmp/foo20121012-6762-12w11to>
t.is_a? File #=> false

2) #dup doesn't duplicate IO

t = Tempfile.new("foo")
t.dup.close
t.read #=> IOError: closed stream

3) finalizer performs unlink even when it has been duplicated

t = Tempfile.new("foo")
path = t.path #=> "/tmp/foo20121012-7533-1q537gq"
File.exist? path #=> true
tt = t.dup
t = nil
GC.start
File.exist? path #=> false

I think these problems caused by using DelegateClass().
Therefore, I made a patch to resolve the problems.
The patched Tempfile class is a subclass of File.


-- 
http://bugs.ruby-lang.org/