Skip to content

Instantly share code, notes, and snippets.

@yaauie
Created October 1, 2015 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yaauie/fa03ef30749b1b1950d7 to your computer and use it in GitHub Desktop.
Save yaauie/fa03ef30749b1b1950d7 to your computer and use it in GitHub Desktop.
#!/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