Module: Rya::CoreExtensions::File

Defined in:
lib/rya/core_extensions.rb

Instance Method Summary collapse

Instance Method Details

#command?(cmd) ⇒ Boolean, String

Check if a string specifies an executable command on the PATH.

Examples:

File.extend Rya::CoreExtensions::File
File.command? "ls" #=> /bin/ls
File.command? "arstoien" #=> nil

Parameters:

  • cmd

    The name of a command to check.

Returns:

  • (Boolean)

    nil if the cmd is not on the PATH and executable

  • (String)

    /path/to/cmd if the cmd is on the PATH and executable



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rya/core_extensions.rb', line 36

def command? cmd
  exts = ENV["PATHEXT"] ? ENV["PATHEXT"].split(";") : [""]

  ENV["PATH"].split(Object::File::PATH_SEPARATOR).each do |path|
    exts.each do |ext|
      exe = Object::File.join path, "#{cmd}#{ext}"

      return exe if Object::File.executable?(exe) && !Object::File.directory?(exe)
    end
  end

  nil
end