module Nuggets::File::WhichMixin

Constants

DEFAULT_EXTENSIONS

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
43 def which(executable, extensions = DEFAULT_EXTENSIONS)
44   extensions |= ['']
45 
46   if env = ::ENV['PATH']
47     dirs = env.split(self::PATH_SEPARATOR)
48     dirs.map! { |dir| expand_path(dir) }
49   end
50 
51   extensions.find { |extension|
52     file = "#{executable}#{extension}"
53     return file if file?(file) && executable?(file)
54 
55     dirs.find { |dir|
56       path = join(dir, file)
57       return path if file?(path) && executable?(path)
58     } if dirs
59   }
60 end
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
66 def which_command(commands, extensions = DEFAULT_EXTENSIONS)
67   commands.find { |command| which(command.to_s[/\S+/], extensions) }
68 end