Class: TTY::System::Which

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/system/which.rb

Overview

A class responsible for finding an executable in the PATH

Instance Method Summary collapse

Instance Method Details

#default_system_pathObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Find default system paths



35
36
37
# File 'lib/tty/system/which.rb', line 35

def default_system_path
  ENV['PATH'].split(File::PATH_SEPARATOR)
end

#which(command) ⇒ String

Find an executable in the PATH

Examples:

which("ruby")  # => /usr/local/bin/ruby

Parameters:

  • command (String)

    the command to search in the PATH

Returns:

  • (String)

    the full path to executable if found, ‘nil` otherwise



21
22
23
24
25
26
27
28
29
30
# File 'lib/tty/system/which.rb', line 21

def which(command)
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  default_system_path.each do |path|
    exts.each do |ext|
      exec = File.join("#{path}", "#{command}#{ext}")
      return exec if File.executable? exec
    end
  end
  return nil
end