diff options
author | Peter Vandenberk <[email protected]> | 2022-09-20 08:53:39 +0100 |
---|---|---|
committer | git <[email protected]> | 2022-10-25 06:59:16 +0000 |
commit | 287c5da4aae6a3c8ae16cde590a9eb6d4dd655bf (patch) | |
tree | 6e9d4401032c1c229703400d9a6840d16a49a85d /lib/tmpdir.rb | |
parent | ba15fb709b0d0dd6310f5cce9ef386bb3bc4707d (diff) |
[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
Diffstat (limited to 'lib/tmpdir.rb')
-rw-r--r-- | lib/tmpdir.rb | 6 |
1 files changed, 2 insertions, 4 deletions
diff --git a/lib/tmpdir.rb b/lib/tmpdir.rb index 6e47ac4fc7..3e76a8f5c3 100644 --- a/lib/tmpdir.rb +++ b/lib/tmpdir.rb @@ -19,8 +19,7 @@ class Dir # Returns the operating system's temporary file path. def self.tmpdir - tmp = nil - ['TMPDIR', 'TMP', 'TEMP', ['system temporary path', @@systmpdir], ['/tmp']*2, ['.']*2].each do |name, dir = ENV[name]| + tmp = ['TMPDIR', 'TMP', 'TEMP', ['system temporary path', @@systmpdir], ['/tmp']*2, ['.']*2].find do |name, dir = ENV[name]| next if !dir dir = File.expand_path(dir) stat = File.stat(dir) rescue next @@ -32,8 +31,7 @@ class Dir when stat.world_writable? && !stat.sticky? warn "#{name} is world-writable: #{dir}" else - tmp = dir - break + break dir end end raise ArgumentError, "could not find a temporary directory" unless tmp |