diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-11-15 08:02:09 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-11-15 08:02:09 +0000 |
commit | 25d56ea7b7b52dc81af30c92a9a0e2d2dab6ff27 (patch) | |
tree | 1a59516f8b50fa62c2b0c02da67162343a3848c6 /lib/tmpdir.rb | |
parent | 0cc57ea01851dd491bd7a4fde80db2b9f0041e4b (diff) |
tmpdir.rb: merged make_tmpname to create
* lib/tmpdir.rb (Dir::Tmpname#create): try conversion of prefix
and suffix just once before loop.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60775 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/tmpdir.rb')
-rw-r--r-- | lib/tmpdir.rb | 22 |
1 files changed, 9 insertions, 13 deletions
diff --git a/lib/tmpdir.rb b/lib/tmpdir.rb index d12afa1ae6..e30004c0bb 100644 --- a/lib/tmpdir.rb +++ b/lib/tmpdir.rb @@ -106,18 +106,6 @@ class Dir Dir.tmpdir end - def make_tmpname((prefix, suffix), n) - prefix = (String.try_convert(prefix) or - raise ArgumentError, "unexpected prefix: #{prefix.inspect}") - suffix &&= (String.try_convert(suffix) or - raise ArgumentError, "unexpected suffix: #{suffix.inspect}") - t = Time.now.strftime("%Y%m%d") - path = "#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}".dup - path << "-#{n}" if n - path << suffix if suffix - path - end - def create(basename, tmpdir=nil, max_try: nil, **opts) if $SAFE > 0 and tmpdir.tainted? tmpdir = '/tmp' @@ -125,8 +113,16 @@ class Dir tmpdir ||= tmpdir() end n = nil + prefix, suffix = basename + prefix = (String.try_convert(prefix) or + raise ArgumentError, "unexpected prefix: #{prefix.inspect}") + suffix &&= (String.try_convert(suffix) or + raise ArgumentError, "unexpected suffix: #{suffix.inspect}") begin - path = File.join(tmpdir, make_tmpname(basename, n)) + t = Time.now.strftime("%Y%m%d") + path = "#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}"\ + "#{n ? %[-#{n}] : ''}#{suffix||''}" + path = File.join(tmpdir, path) yield(path, n, opts) rescue Errno::EEXIST n ||= 0 |