[前][次][番号順一覧][スレッド一覧]

ruby-changes:28341

From: akr <ko1@a...>
Date: Sat, 20 Apr 2013 22:51:00 +0900 (JST)
Subject: [ruby-changes:28341] akr:r40393 (trunk): * lib/tempfile.rb (Tempfile.create): New method.

akr	2013-04-20 22:50:47 +0900 (Sat, 20 Apr 2013)

  New Revision: 40393

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=40393

  Log:
    * lib/tempfile.rb (Tempfile.create): New method.
      The method name is proposed by Shugo Maeda.  [ruby-dev:47220]
      [ruby-core:41478] [Feature #5707]

  Modified files:
    trunk/ChangeLog
    trunk/NEWS
    trunk/lib/tempfile.rb
    trunk/test/test_tempfile.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 40392)
+++ ChangeLog	(revision 40393)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Apr 20 22:47:48 2013  Tanaka Akira  <akr@f...>
+
+	* lib/tempfile.rb (Tempfile.create): New method.
+	  The method name is proposed by Shugo Maeda.  [ruby-dev:47220]
+	  [ruby-core:41478] [Feature #5707]
+
 Sat Apr 20 14:22:10 2013  Nobuyoshi Nakada  <nobu@r...>
 
 	* marshal.c (w_object): dump no ivars to the original by marshal_dump.
Index: lib/tempfile.rb
===================================================================
--- lib/tempfile.rb	(revision 40392)
+++ lib/tempfile.rb	(revision 40393)
@@ -332,6 +332,51 @@ class Tempfile < DelegateClass(File) https://github.com/ruby/ruby/blob/trunk/lib/tempfile.rb#L332
   end
 end
 
+# Creates a temporally file as usual File object (not Tempfile).
+# It don't use finalizer and delegation.
+#
+# If no block is given, this is similar to Tempfile.new except
+# creating File instead of Tempfile.
+# 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 temporally file is removed after the block terminates.
+# The call returns the value of the block.
+#
+# In any case, all arguments (+*args+) will be treated as Tempfile.new.
+#
+#   Tempfile.create('foo', '/home/temp') do |f|
+#      ... do something with f ...
+#   end
+#
+def Tempfile.create(basename, *rest)
+  tmpfile = nil
+  Dir::Tmpname.create(basename, *rest) do |tmpname, n, opts|
+    mode = File::RDWR|File::CREAT|File::EXCL
+    perm = 0600
+    if opts
+      mode |= opts.delete(:mode) || 0
+      opts[:perm] = perm
+      perm = nil
+    else
+      opts = perm
+    end
+    tmpfile = File.open(tmpname, mode, opts)
+  end
+  if block_given?
+    begin
+      yield tmpfile
+    ensure
+      File.unlink tmpfile
+    end
+  else
+    tmpfile
+  end
+end
+
 if __FILE__ == $0
 #  $DEBUG = true
   f = Tempfile.new("foo")
Index: NEWS
===================================================================
--- NEWS	(revision 40392)
+++ NEWS	(revision 40393)
@@ -70,5 +70,9 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L70
   * Rinda now supports multicast sockets.  See Rinda::RingServer and
     Rinda::RingFinger for details.
 
+* Tempfile
+  * New methods:
+    * Tempfile.create
+
 === Stdlib compatibility issues (excluding feature bug fixes)
 === C API updates
Index: test/test_tempfile.rb
===================================================================
--- test/test_tempfile.rb	(revision 40392)
+++ test/test_tempfile.rb	(revision 40393)
@@ -304,5 +304,26 @@ puts Tempfile.new('foo').path https://github.com/ruby/ruby/blob/trunk/test/test_tempfile.rb#L304
       assert_equal(0600, t.stat.mode & 0777)
     end
   end
+
+  def test_create_with_block
+    path = nil
+    Tempfile.create("tempfile-create") {|f|
+      path = f.path
+      assert(File.exist?(path))
+    }
+    assert(!File.exist?(path))
+  end
+
+  def test_create_without_block
+    path = nil
+    f = Tempfile.create("tempfile-create")
+    path = f.path
+    assert(File.exist?(path))
+    f.close
+    assert(File.exist?(path))
+  ensure
+    f.close if f && !f.closed?
+    File.unlink path if path
+  end
 end
 

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]