class Bundler::RubygemsIntegration

def replace_gem(specs)

def replace_gem(specs)
  reverse_rubygems_kernel_mixin
  executables = specs.map(&:executables).flatten
  kernel = (class << ::Kernel; self; end)
  [kernel, ::Kernel].each do |kernel_class|
    redefine_method(kernel_class, :gem) do |dep, *reqs|
      if executables.include? File.basename(caller.first.split(":").first)
        break
      end
      reqs.pop if reqs.last.is_a?(Hash)
      unless dep.respond_to?(:name) && dep.respond_to?(:requirement)
        dep = Gem::Dependency.new(dep, reqs)
      end
      spec = specs.find {|s| s.name == dep.name }
      if spec.nil?
        e = Gem::LoadError.new "#{dep.name} is not part of the bundle. Add it to Gemfile."
        e.name = dep.name
        if e.respond_to?(:requirement=)
          e.requirement = dep.requirement
        else
          e.version_requirement = dep.requirement
        end
        raise e
      elsif dep !~ spec
        e = Gem::LoadError.new "can't activate #{dep}, already activated #{spec.full_name}. " \
                               "Make sure all dependencies are added to Gemfile."
        e.name = dep.name
        if e.respond_to?(:requirement=)
          e.requirement = dep.requirement
        else
          e.version_requirement = dep.requirement
        end
        raise e
      end
      true
    end
  end
end