Sha256: 2f96edcf02c94fb6868977b8ecd3d6e9693deeecd9bcaf0bbf763b62211036c3

Contents?: true

Size: 1.17 KB

Versions: 4

Compression:

Stored size: 1.17 KB

Contents

# encoding: utf-8

module TTY
  class System
    # A class responsible for finding an executable in the PATH
    class Which
      # The command to find
      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
        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

4 entries across 4 versions & 1 rubygems

Version Path
tty-0.1.3 lib/tty/system/which.rb
tty-0.1.2 lib/tty/system/which.rb
tty-0.1.1 lib/tty/system/which.rb
tty-0.1.0 lib/tty/system/which.rb