[ruby/tmpdir] Make Dir.tmpdir more idiomatic and functional

Use `Enumerable#find` to iterate over the candidates, not `Enumerable.each`.

(this makes the code more functional, and - IMO - slightly more idiomatic,
as it avoids setting the "global" (by which I mean: non-local) `tmp`
variable from inside the block)

https://github.com/ruby/tmpdir/commit/d1f20ad694
This commit is contained in:
Peter Vandenberk 2022-09-20 08:53:39 +01:00 committed by git
parent ba15fb709b
commit 287c5da4aa

View File

@ -19,8 +19,7 @@ class Dir
# Returns the operating system's temporary file path. # Returns the operating system's temporary file path.
def self.tmpdir def self.tmpdir
tmp = nil tmp = ['TMPDIR', 'TMP', 'TEMP', ['system temporary path', @@systmpdir], ['/tmp']*2, ['.']*2].find do |name, dir = ENV[name]|
['TMPDIR', 'TMP', 'TEMP', ['system temporary path', @@systmpdir], ['/tmp']*2, ['.']*2].each do |name, dir = ENV[name]|
next if !dir next if !dir
dir = File.expand_path(dir) dir = File.expand_path(dir)
stat = File.stat(dir) rescue next stat = File.stat(dir) rescue next
@ -32,8 +31,7 @@ class Dir
when stat.world_writable? && !stat.sticky? when stat.world_writable? && !stat.sticky?
warn "#{name} is world-writable: #{dir}" warn "#{name} is world-writable: #{dir}"
else else
tmp = dir break dir
break
end end
end end
raise ArgumentError, "could not find a temporary directory" unless tmp raise ArgumentError, "could not find a temporary directory" unless tmp