Class: Quality::Which

Inherits:
Object
  • Object
show all
Defined in:
lib/quality/which.rb

Overview

Determine where a given executable lives, like the UNIX ‘which’ command

Instance Method Summary collapse

Constructor Details

#initialize(env: ENV, file: File, separator: File::PATH_SEPARATOR) ⇒ Which

Returns a new instance of Which.



8
9
10
11
12
13
14
# File 'lib/quality/which.rb', line 8

def initialize(env: ENV,
               file: File,
               separator: File::PATH_SEPARATOR)
  @env = env
  @file = file
  @separator = separator
end

Instance Method Details

#which(cmd) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/quality/which.rb', line 16

def which(cmd)
  exts = @env['PATHEXT'] ? @env['PATHEXT'].split(';') : ['']
  @env['PATH'].split(@separator).each do |path|
    exts.each do |ext|
      exe = @file.join(path, "#{cmd}#{ext}")
      return exe if @file.executable?(exe) && !@file.directory?(exe)
    end
  end
  nil
end