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

ruby-changes:35573

From: glass <ko1@a...>
Date: Sun, 21 Sep 2014 02:35:20 +0900 (JST)
Subject: [ruby-changes:35573] glass:r47655 (trunk): * lib/tempfile.rb: define parameters appropriately and some

glass	2014-09-21 02:35:06 +0900 (Sun, 21 Sep 2014)

  New Revision: 47655

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

  Log:
    * lib/tempfile.rb: define parameters appropriately and some
                       refactoring.
    
    * lib/tmpdir.rb:   ditto.

  Modified files:
    trunk/ChangeLog
    trunk/lib/tempfile.rb
    trunk/lib/tmpdir.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 47654)
+++ ChangeLog	(revision 47655)
@@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Sep 20 03:00:26 2014  Masaki Matsushita <glass.saga@g...>
+
+	* lib/tempfile.rb: define parameters appropriately and some
+	                   refactoring.
+
+	* lib/tmpdir.rb:   ditto.
+
 Sat Sep 20 23:58:21 2014  Tanaka Akira  <akr@f...>
 
 	* enum.c (enum_chunk): Deprecate the state management.
Index: lib/tempfile.rb
===================================================================
--- lib/tempfile.rb	(revision 47654)
+++ lib/tempfile.rb	(revision 47655)
@@ -78,8 +78,6 @@ require 'tmpdir' https://github.com/ruby/ruby/blob/trunk/lib/tempfile.rb#L78
 # same Tempfile object from multiple threads then you should protect it with a
 # mutex.
 class Tempfile < DelegateClass(File)
-  include Dir::Tmpname
-
   # call-seq:
   #    new(basename, [tmpdir = Dir.tmpdir], [options])
   #
@@ -124,7 +122,7 @@ class Tempfile < DelegateClass(File) https://github.com/ruby/ruby/blob/trunk/lib/tempfile.rb#L122
   #
   # If Tempfile.new cannot find a unique filename within a limited
   # number of tries, then it will raise an exception.
-  def initialize(basename, *rest)
+  def initialize(basename, tmpdir=nil, mode: 0, **opts)
     if block_given?
       warn "Tempfile.new doesn't call the given block."
     end
@@ -132,20 +130,13 @@ class Tempfile < DelegateClass(File) https://github.com/ruby/ruby/blob/trunk/lib/tempfile.rb#L130
     @clean_proc = Remover.new(@data)
     ObjectSpace.define_finalizer(self, @clean_proc)
 
-    ::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
+    ::Dir::Tmpname.create(basename, tmpdir, opts) do |tmpname, n, opts|
+      mode |= File::RDWR|File::CREAT|File::EXCL
+      opts[:perm] = 0600
       @data[1] = @tmpfile = File.open(tmpname, mode, opts)
       @data[0] = @tmpname = tmpname
       @mode = mode & ~(File::CREAT|File::EXCL)
-      perm or opts.freeze
+      opts.freeze
       @opts = opts
     end
 
@@ -278,7 +269,7 @@ class Tempfile < DelegateClass(File) https://github.com/ruby/ruby/blob/trunk/lib/tempfile.rb#L269
     def call(*args)
       return if @pid != $$
 
-      path, tmpfile = *@data
+      path, tmpfile = @data
 
       STDERR.print "removing ", path, "..." if $DEBUG
 
@@ -356,18 +347,11 @@ end https://github.com/ruby/ruby/blob/trunk/lib/tempfile.rb#L347
 #      ... do something with f ...
 #   end
 #
-def Tempfile.create(basename, *rest)
+def Tempfile.create(basename, tmpdir=nil, mode: 0, **opts)
   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
+  Dir::Tmpname.create(basename, tmpdir, opts) do |tmpname, n, opts|
+    mode |= File::RDWR|File::CREAT|File::EXCL
+    opts[:perm] = 0600
     tmpfile = File.open(tmpname, mode, opts)
   end
   if block_given?
Index: lib/tmpdir.rb
===================================================================
--- lib/tmpdir.rb	(revision 47654)
+++ lib/tmpdir.rb	(revision 47655)
@@ -17,12 +17,12 @@ class Dir https://github.com/ruby/ruby/blob/trunk/lib/tmpdir.rb#L17
   ##
   # Returns the operating system's temporary file path.
 
-  def Dir::tmpdir
+  def self.tmpdir
     if $SAFE > 0
       @@systmpdir
     else
       tmp = nil
-      for dir in [ENV['TMPDIR'], ENV['TMP'], ENV['TEMP'], @@systmpdir, '/tmp', '.']
+      [ENV['TMPDIR'], ENV['TMP'], ENV['TEMP'], @@systmpdir, '/tmp', '.'].each do |dir|
         next if !dir
         dir = File.expand_path(dir)
         if stat = File.stat(dir) and stat.directory? and stat.writable? and
@@ -31,7 +31,7 @@ class Dir https://github.com/ruby/ruby/blob/trunk/lib/tmpdir.rb#L31
           break
         end rescue nil
       end
-      raise ArgumentError, "could not find a temporary directory" if !tmp
+      raise ArgumentError, "could not find a temporary directory" unless tmp
       tmp
     end
   end
@@ -81,8 +81,8 @@ class Dir https://github.com/ruby/ruby/blob/trunk/lib/tmpdir.rb#L81
   #    FileUtils.remove_entry dir
   #  end
   #
-  def Dir.mktmpdir(prefix_suffix=nil, *rest)
-    path = Tmpname.create(prefix_suffix || "d", *rest) {|n| mkdir(n, 0700)}
+  def Dir.mktmpdir(prefix_suffix="d", *rest)
+    path = Tmpname.create(prefix_suffix, *rest) {|n| mkdir(n, 0700)}
     if block_given?
       begin
         yield path
@@ -122,15 +122,7 @@ class Dir https://github.com/ruby/ruby/blob/trunk/lib/tmpdir.rb#L122
       path << suffix
     end
 
-    def create(basename, *rest)
-      if opts = Hash.try_convert(rest[-1])
-        opts = opts.dup if rest.pop.equal?(opts)
-        max_try = opts.delete(:max_try)
-        opts = [opts]
-      else
-        opts = []
-      end
-      tmpdir, = *rest
+    def create(basename, tmpdir=nil, max_try: nil, **opts)
       if $SAFE > 0 and tmpdir.tainted?
         tmpdir = '/tmp'
       else
@@ -139,7 +131,7 @@ class Dir https://github.com/ruby/ruby/blob/trunk/lib/tmpdir.rb#L131
       n = nil
       begin
         path = File.join(tmpdir, make_tmpname(basename, n))
-        yield(path, n, *opts)
+        yield(path, n, opts)
       rescue Errno::EEXIST
         n ||= 0
         n += 1

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

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