Skip to content

Instantly share code, notes, and snippets.

@larsch
Last active December 23, 2015 19:39
Show Gist options
  • Select an option

  • Save larsch/6683865 to your computer and use it in GitHub Desktop.

Select an option

Save larsch/6683865 to your computer and use it in GitHub Desktop.
Portable which
def which(file)
exts = [""] + (ENV["PATHEXT"]||"").split(File::PATH_SEPARATOR)
ENV["PATH"].split(File::PATH_SEPARATOR).map do |path|
exts.map do |ext|
filepath = File.join(path, file + ext)
defined?(File::ALT_SEPARATOR) && File::ALT_SEPARATOR && filepath.tr!(File::SEPARATOR, File::ALT_SEPARATOR)
filepath if File.exist?(filepath)
end
end.flatten.compact.first
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment