This code is based off of what @Kashyap answered (Bundler::LockfileParser is a good find). I ended up changing it a bit and wanted to share what I ended up using.
require 'rubygems'
require 'bundler'
LOCK_FILES = %w(/path/to/first/Gemfile.lock /path/to/second/Gemfile.lock)
REVIEWABLE_SHELL_SCRIPT = 'gem_cleaner.csh'
class GemCleaner
def initialize lock_files, output_file
@lock_files = lock_files
@output_file = output_file
end
def lock_file_gems
@lock_files.map do |lock_file|
Bundler::LockfileParser.new(File.read(lock_file)).specs.
map {|s| [s.name, s.version.version] }
end.flatten(1).uniq
end
def installed_gems
Gem::Specification.find_all.map {|s| [s.name, s.version.version] }
end
def gems_to_uninstall
installed_gems - lock_file_gems
end
def create_shell_script
File.open(@output_file, 'w', 0744) do |f|
f.puts "#!/bin/csh"
gems_to_uninstall.sort.each {|g| f.puts "gem uninstall #{g[0]} -v #{g[1]}" }
end
end
end
gc = GemCleaner.new(LOCK_FILES, REVIEWABLE_SHELL_SCRIPT)
gc.create_shell_script
Primary differences are use of Gem::Specification.find_all and output to a shell script so I could review the gems before uninstalling. Oh, and still doing it the old-fashioned OO-way. :)
Leaving selected answer with @Kashyap. Props.