https://github.com/alphahydrae/which_works
Ruby UNIX-like which.
https://github.com/alphahydrae/which_works
Last synced: 3 months ago
JSON representation
Ruby UNIX-like which.
- Host: GitHub
- URL: https://github.com/alphahydrae/which_works
- Owner: AlphaHydrae
- License: mit
- Created: 2012-03-09T21:01:50.000Z (over 14 years ago)
- Default Branch: master
- Last Pushed: 2013-10-04T15:14:51.000Z (almost 13 years ago)
- Last Synced: 2025-02-20T16:18:17.370Z (over 1 year ago)
- Language: Ruby
- Homepage:
- Size: 133 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# which\_works
**Ruby UNIX-like which. Locates a program file in the user's path.**
[](http://badge.fury.io/rb/which\_works)
[](https://gemnasium.com/AlphaHydrae/which\_works)
[](http://travis-ci.org/AlphaHydrae/which\_works)
[](https://coveralls.io/r/AlphaHydrae/which\_works?branch=master)
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.
```ruby
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](https://raw.github.com/AlphaHydrae/which_works/master/LICENSE.txt))