module Nuggets::File::WhichMixin



Public Instance Methods

which(executable[, extensions]) → aString or +nil+ click to toggle source

Returns executable if it's executable, or the full path to executable found in PATH, or nil otherwise. Checks executable with each extension in extensions appended in turn.

Inspired by Gnuplot.which – thx, Gordon!

# File lib/nuggets/file/which_mixin.rb, line 43
def which(executable, extensions = DEFAULT_EXTENSIONS)
  extensions |= ['']

  if env = ::ENV['PATH']
    dirs = env.split(self::PATH_SEPARATOR)! { |dir| expand_path(dir) }

  extensions.find { |extension|
    file = "#{executable}#{extension}"
    return file if file?(file) && executable?(file)

    dirs.find { |dir|
      path = join(dir, file)
      return path if file?(path) && executable?(path)
    } if dirs
which_command(commands) → aString or +nil+ click to toggle source

Returns the first of commands that is executable (according to which).

# File lib/nuggets/file/which_mixin.rb, line 66
def which_command(commands, extensions = DEFAULT_EXTENSIONS)
  commands.find { |command| which(command.to_s[/\S+/], extensions) }