module Pry::Rubygem - pry-0.10.1 Documentation

# module Pry::Rubygem

### Public Class Methods

complete(so_far) click to toggle source

Completion function for gem-cd and gem-open.

@param [String] so_far what the user’s typed so far @return [Array<String>] completions

    # File lib/pry/rubygem.rb, line 47
    def complete(so_far)
      if so_far =~ / ([^ ]*)\z/
        self.list(%r{\A#{$2}}).map(&:name)
      else
        self.list.map(&:name)
      end
    end

install(name) click to toggle source

Installs a gem with all its dependencies.

@param [String] name @return [void]

    # File lib/pry/rubygem.rb, line 59
    def install(name)
      gemrc_opts = Gem.configuration['gem'].split(' ')
      destination = if gemrc_opts.include?('--user-install')
                      Gem.user_dir
                    elsif File.writable?(Gem.dir)
                      Gem.dir
                    else
                      Gem.user_dir
                    end
      installer = Gem::DependencyInstaller.new(:install_dir => destination)
      installer.install(name)
    rescue Errno::EACCES
      raise CommandError,
        "Insufficient permissions to install #{ Pry::Helpers::Text.green(name) }."
    rescue Gem::GemNotFoundException
      raise CommandError,
        "Gem #{ Pry::Helpers::Text.green(name) } not found. Aborting installation."
    else
      Gem.refresh
    end

installed?(name) click to toggle source

    # File lib/pry/rubygem.rb, line 7
    def installed?(name)
      if Gem::Specification.respond_to?(:find_all_by_name)
        Gem::Specification.find_all_by_name(name).any?
      else
        Gem.source_index.find_name(name).first
      end
    end

list(pattern = /.*/) click to toggle source

List gems matching a pattern.

@param [Regexp] pattern @return [Array<Gem::Specification>]

    # File lib/pry/rubygem.rb, line 35
    def list(pattern = /.*/)
      if Gem::Specification.respond_to?(:each)
        Gem::Specification.select{|spec| spec.name =~ pattern }
      else
        Gem.source_index.gems.values.select{|spec| spec.name =~ pattern }
      end
    end

spec(name) click to toggle source

Get the gem spec object for the given gem name.

@param [String] name @return [Gem::Specification]

    # File lib/pry/rubygem.rb, line 19
    def spec(name)
      specs = if Gem::Specification.respond_to?(:each)
                Gem::Specification.find_all_by_name(name)
              else
                Gem.source_index.find_name(name)
              end

      first_spec = specs.sort_by{ |spec| Gem::Version.new(spec.version) }.last

      first_spec or raise CommandError, "Gem `#{name}` not found"
    end