Module: Readapt::Finder

Included in:
Debugger
Defined in:
lib/readapt/finder.rb

Overview

Methods to find a program in the current directory or one of the environment paths.

Class Method Summary collapse

Class Method Details

.find(program) ⇒ String

Get the program’s fully qualified path. Search first in the current directory, then the environment paths.

Parameters:

  • program (String)

    The name of the program

Returns:

  • (String)

    The fully qualified path

Raises:

  • (LoadError)

    if the program was not found



17
18
19
20
# File 'lib/readapt/finder.rb', line 17

def find program
  return program if File.exist?(program)
  which(program) || raise(LoadError, "#{program} is not a valid Ruby file or program")
end

.which(program) ⇒ String

Search the environment paths for the given program.

Parameters:

  • program (String)

    The name of the program

Returns:

  • (String)

    The fully qualified path, or nil if it was not found



26
27
28
29
30
31
32
# File 'lib/readapt/finder.rb', line 26

def which program
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exe = File.join(path, program)
    return exe if File.file?(exe)
  end
  nil
end