Created
March 26, 2014 05:27
-
-
Save mcpate/9777341 to your computer and use it in GitHub Desktop.
Cross-platform way of finding an executable in the $PATH.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Cross-platform way of finding an executable in the $PATH. | |
# | |
# which('ruby') #=> /usr/bin/ruby | |
def which(cmd) | |
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : [''] | |
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path| | |
exts.each { |ext| | |
exe = File.join(path, "#{cmd}#{ext}") | |
return exe if File.executable? exe | |
} | |
end | |
return nil | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment