class Gem::Commands::ExecCommand

def load!

def load!
  argv = ARGV.clone
  ARGV.replace options[:args]
  executable = options[:executable]
  contains_executable = Gem.loaded_specs.values.select do |spec|
    spec.executables.include?(executable)
  end
  if contains_executable.any? {|s| s.name == executable }
    contains_executable.select! {|s| s.name == executable }
  end
  if contains_executable.empty?
    spec = Gem.loaded_specs[executable]
    if spec.nil? || spec.executables.empty?
      alert_error "Failed to load executable `#{executable}`," \
            " are you sure the gem `#{options[:gem_name]}` contains it?"
      terminate_interaction 1
    end
    if spec.executables.size > 1
      alert_error "Ambiguous which executable from gem `#{executable}` should be run: " \
            "the options are #{spec.executables.sort}, specify one via COMMAND, and use `-g` and `-v` to specify gem and version"
      terminate_interaction 1
    end
    contains_executable << spec
    executable = spec.executable
  end
  if contains_executable.size > 1
    alert_error "Ambiguous which gem `#{executable}` should come from: " \
          "the options are #{contains_executable.map(&:name)}, " \
          "specify one via `-g`"
    terminate_interaction 1
  end
  old_exe = $0
  $0 = executable
  load Gem.activate_bin_path(contains_executable.first.name, executable, ">= 0.a")
ensure
  $0 = old_exe if old_exe
  ARGV.replace argv
end