Skip to content

Commit d219ee2

Browse files
committed
Move private constants under Dir::Tmpname module
Including `TMPDIR_CANDIDATES` extracted from `Dir.tmpdir` invariant.
1 parent db43e53 commit d219ee2

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

lib/tmpdir.rb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,14 @@ class Dir
1616
# Class variables are inaccessible from non-main Ractor.
1717
# And instance variables too, in Ruby 3.0.
1818

19-
# System-wide temporary directory path
20-
SYSTMPDIR = (defined?(Etc.systmpdir) ? Etc.systmpdir.freeze : '/tmp')
21-
private_constant :SYSTMPDIR
22-
2319
##
2420
# Returns the operating system's temporary file path.
2521
#
2622
# require 'tmpdir'
2723
# Dir.tmpdir # => "/tmp"
2824

2925
def self.tmpdir
30-
['TMPDIR', 'TMP', 'TEMP', ['system temporary path', SYSTMPDIR], ['/tmp']*2, ['.']*2].find do |name, dir|
26+
Tmpname::TMPDIR_CANDIDATES.find do |name, dir|
3127
unless dir
3228
next if !(dir = ENV[name] rescue next) or dir.empty?
3329
end
@@ -126,6 +122,18 @@ def self.mktmpdir(prefix_suffix=nil, *rest, **options, &block)
126122
module Tmpname # :nodoc:
127123
module_function
128124

125+
# System-wide temporary directory path
126+
systmpdir = (defined?(Etc.systmpdir) ? Etc.systmpdir.freeze : '/tmp')
127+
128+
# Temporary directory candidates consisting of environment variable
129+
# names or description and path pairs.
130+
TMPDIR_CANDIDATES = [
131+
'TMPDIR', 'TMP', 'TEMP',
132+
['system temporary path', systmpdir],
133+
%w[/tmp /tmp],
134+
%w[. .],
135+
].each(&:freeze).freeze
136+
129137
def tmpdir
130138
Dir.tmpdir
131139
end

0 commit comments

Comments
 (0)