# File lib/rubygems/commands/update_command.rb, line 130
  def update_rubygems
    unless options[:args].empty? then
      alert_error "Gem names are not allowed with the --system option"
      terminate_interaction 1
    end

    options[:user_install] = false

    # TODO: rename version and other variable name conflicts
    # TODO: get rid of all this indirection on name and other BS

    version = options[:system]
    if version == true then
      version     = Gem::Version.new     Gem::VERSION
      requirement = Gem::Requirement.new ">= #{Gem::VERSION}"
    else
      version     = Gem::Version.new     version
      requirement = Gem::Requirement.new version
    end

    rubygems_update         = Gem::Specification.new
    rubygems_update.name    = 'rubygems-update'
    rubygems_update.version = version

    hig = {
      'rubygems-update' => rubygems_update
    }

    gems_to_update = which_to_update hig, options[:args], :system
    name, up_ver   = gems_to_update.first
    current_ver    = Gem::Version.new Gem::VERSION

    target = if options[:system] == true then
               up_ver
             else
               version
             end

    if current_ver == target then
      # if options[:system] != true and version == current_ver then
      say "Latest version currently installed. Aborting."
      terminate_interaction
    end

    update_gem name, target

    installed_gems = Gem::Specification.find_all_by_name 'rubygems-update', requirement
    version        = installed_gems.last.version

    args = []
    args << '--prefix' << Gem.prefix if Gem.prefix
    args << '--no-rdoc' unless options[:generate_rdoc]
    args << '--no-ri' unless options[:generate_ri]
    args << '--no-format-executable' if options[:no_format_executable]

    update_dir = File.join Gem.dir, 'gems', "rubygems-update-#{version}"

    Dir.chdir update_dir do
      say "Installing RubyGems #{version}"
      setup_cmd = "#{Gem.ruby} setup.rb #{args.join ' '}"

      # Make sure old rubygems isn't loaded
      old = ENV["RUBYOPT"]
      ENV.delete("RUBYOPT") if old
      installed = system setup_cmd
      say "RubyGems system software updated" if installed
      ENV["RUBYOPT"] = old if old
    end
  end