Skip to content

Commit d1f20ad

Browse files
author
Peter Vandenberk
committed
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)
1 parent c480226 commit d1f20ad

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

lib/tmpdir.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ class Dir
1919
# Returns the operating system's temporary file path.
2020

2121
def self.tmpdir
22-
tmp = nil
23-
['TMPDIR', 'TMP', 'TEMP', ['system temporary path', @@systmpdir], ['/tmp']*2, ['.']*2].each do |name, dir = ENV[name]|
22+
tmp = ['TMPDIR', 'TMP', 'TEMP', ['system temporary path', @@systmpdir], ['/tmp']*2, ['.']*2].find do |name, dir = ENV[name]|
2423
next if !dir
2524
dir = File.expand_path(dir)
2625
stat = File.stat(dir) rescue next
@@ -32,8 +31,7 @@ def self.tmpdir
3231
when stat.world_writable? && !stat.sticky?
3332
warn "#{name} is world-writable: #{dir}"
3433
else
35-
tmp = dir
36-
break
34+
break dir
3735
end
3836
end
3937
raise ArgumentError, "could not find a temporary directory" unless tmp

0 commit comments

Comments
 (0)