Skip to content

Instantly share code, notes, and snippets.

@mcpate
Created March 26, 2014 05:27
Show Gist options
  • Save mcpate/9777341 to your computer and use it in GitHub Desktop.
Save mcpate/9777341 to your computer and use it in GitHub Desktop.
Cross-platform way of finding an executable in the $PATH.
# 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