Skip to content

File.which

Daniel Berger edited this page Sep 13, 2019 · 7 revisions

File.which

As any Unix user knows, the which command is a handy shell command for finding where a given binary is, if it can be found at all, e.g. which ruby => /usr/bin/ruby. In cases where multiple versions exist, it will tell you which one is first within your PATH.

With the ptools gem, you can use the File.which method to achieve the same behavior. You may optionally specify a custom PATH setting that will be used instead of your default environment settings.

# Examples:

# => '/usr/bin/ruby'
File.which('ruby')

# => Only search the specified path
File.which('ruby', '/usr/bin;/usr/sbin')

MS Windows

On MS Windows, you can optionally omit the extension for the executable that you're searching for. So, you can do File.which('ruby') or File.which('ruby.exe') as you prefer.

It will automatically look for an executable with .exe, .com or .bat extensions (in that order) if you do not supply one.

Clone this wiki locally