which_works
Ruby UNIX-like which. Locates a program file in the user's path.
The which
method takes a list of command names and searches the path
for each executable file that would be run had these commands actually
been invoked.
Which.which('ls') #=> "/bin/ls"
Which.which('ls', 'screen') #=> [ "/bin/ls", "/usr/bin/screen" ]
Which.which('unknown') #=> nil
# you can also check an absolute path
Which.which('/usr/bin/svn') #=> "/usr/bin/svn"
Which.which('/usr/bin/foo') #=> nil
# the :all option returns all executable files,
# not just the first one found in the path
Which.which('svn', :all => true) #=> [ "/opt/local/bin/svn", "/usr/bin/svn" ]
# the :array option always returns an array
Which.which('unknown', :array => true) #=> []
Which.which('ls', :array => true) #=> [ "/bin/ls" ]
Which.which('ls', 'screen', :array => true) #=> [ "/bin/ls", "/usr/bin/screen" ]
# combined options
Which.which('ls', 'svn', :all => true, :array => true)
#=> [ "/bin/ls", "/opt/local/bin/svn", "/usr/bin/svn" ]
# you can change the default options
Which.options = { :all => true }
Which.options[:array] = true
Which.which('ls') #=> [ "/bin/ls" ]
Which.which('svn') #=> [ "/opt/local/bin/svn", "/usr/bin/svn" ]
# default options can be overridden as usual
Which.which('ls', :array => false) #=> "/bin/ls"
# see the current default options
Which.options #=> { :all => true, :array => true }
Meta
- Author: Simon Oulevay (Alpha Hydrae)
- License: MIT (see LICENSE.txt)