Skip to content

Instantly share code, notes, and snippets.

@wtnabe
Created August 11, 2009 14:21
Show Gist options
  • Save wtnabe/165860 to your computer and use it in GitHub Desktop.
Save wtnabe/165860 to your computer and use it in GitHub Desktop.
"which" command Ruby implementation
#! /usr/bin/env ruby
def which( cmd, *additional )
paths = ENV['PATH'].split( File::PATH_SEPARATOR )
paths += additional if additional
path = paths.map { |d|
file = File.join( d, cmd )
( File.exist?( file ) ) ? file : nil
}.compact.first
return ( path ) ? path : "command not found : #{cmd}"
end
puts which( ARGV[0], *ARGV[1..ARGV.size-1] )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment