Created
October 1, 2015 17:40
-
-
Save yaauie/fa03ef30749b1b1950d7 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/ruby | |
# encoding: utf-8 | |
module Where | |
extend self | |
def where(cmd) | |
return enum_for(:where, cmd) unless block_given? | |
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : [''] | |
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path| | |
exts.each do |ext| | |
exe = File.join(path, "#{cmd}#{ext}") | |
yield exe if File.executable? exe | |
end | |
end | |
end | |
def which(cmd) | |
where(cmd).first | |
end | |
end | |
raise "Usage: \`#{$0}\` executable-name" unless ARGV.size == 1 | |
puts (Where::where(ARGV.first).to_a.tap { |result| exit 1 if result.empty? }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment