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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command) ⇒ Which

Initialize a Which

Parameters:

  • command (String)

    the command to find



17
18
19
# File 'lib/tty/system/which.rb', line 17

def initialize(command)
  @command = command
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



9
10
11
# File 'lib/tty/system/which.rb', line 9

def command
  @command
end

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



47
48
49
# File 'lib/tty/system/which.rb', line 47

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

#whichString

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



33
34
35
36
37
38
39
40
41
42
# File 'lib/tty/system/which.rb', line 33

def which
  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