Issue #7767 has been updated by yhara (Yutaka HARA).

Category set to lib
Target version set to next minor


----------------------------------------
Feature #7767: Tempfile????????????????????????????
https://bugs.ruby-lang.org/issues/7767#change-36219

Author: kyanagi (Kouhei Yanagita)
Status: Open
Priority: Normal
Assignee: 
Category: lib
Target version: next minor


????????????????tempfile.rb???Good practices????????????????????????????????
Tempfile??使???????????????????????????????奨????????????????

????????????Tempfile.open????????????渡?????????????close??????????????????????????????????
??????????????????????????????????????????????????????????念????????????????

Tempfile.open????????????????????????????????????????close!?????????????????????????????????
???????????使????????????????????使???????????????????????????????????????????????????????????????

?????????????????????
* 使???????????????????????????open????????????????????????????????????????????????
* Tempfile.open????????????????????????????close???????????????????????????????????close!??????
???????????????????????????Tempfile.open!???????????????????



Index: lib/tempfile.rb
===================================================================
--- lib/tempfile.rb	(revision 39003)
+++ lib/tempfile.rb	(working copy)
@@ -305,6 +305,9 @@
     #
     # In any case, all arguments (+*args+) will be passed to Tempfile.new.
     #
+    # +open!+ is same as +open+, except that the file will be deleted immediately
+    # after the block terminates.
+    #
     #   Tempfile.open('foo', '/home/temp') do |f|
     #      ... do something with f ...
     #   end
@@ -316,14 +319,23 @@
     #   ensure
     #      f.close
     #   end
-    def open(*args)
+    def open(*args, &block)
+      _open(false, *args, &block)
+    end
+
+    def open!(*args, &block)
+      _open(true, *args, &block)
+    end
+
+    private
+    def _open(unlink_at_block_end, *args, &block)
       tempfile = new(*args)
 
       if block_given?
         begin
           yield(tempfile)
         ensure
-          tempfile.close
+          tempfile.close(unlink_at_block_end)
         end
       else
         tempfile
Index: test/test_tempfile.rb
===================================================================
--- test/test_tempfile.rb	(revision 39003)
+++ test/test_tempfile.rb	(working copy)
@@ -304,5 +304,13 @@
       assert_equal(0600, t.stat.mode & 0777)
     end
   end
+
+  def test_open_bang_with_block
+    path = nil
+    Tempfile.open!('foo') do |f|
+      path = f.path
+    end
+    assert !File.exist?(path)
+  end
 end
 



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