Sha256: 419c23ddb803cd63e9546e40c39c005cbf345fa4cef14bd7050c4923914f5338

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

# -*- encoding: utf-8 -*-

module TTY
  class System

    # A class responsible for finding an executable in the PATH
    class Which

      attr_reader :command

      # Initialize a Which
      #
      # @param [String] command
      #   the command to find
      #
      # @api public
      def initialize(command)
        @command = command
      end

      # Find an executable in the PATH
      #
      # @param [String] command
      #   the command to search in the PATH
      #
      # @example
      #   which("ruby")  # => /usr/local/bin/ruby
      #
      # @return [String]
      #   the full path to executable if found, `nil` otherwise
      #
      # @api public
      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

      # Find default system paths
      #
      # @api private
      def default_system_path
        ENV['PATH'].split(File::PATH_SEPARATOR)
      end

    end # Which
  end # System
end # TTY

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tty-0.0.11 lib/tty/system/which.rb
tty-0.0.10 lib/tty/system/which.rb