Skip to content

Instantly share code, notes, and snippets.

@adamjmurray
Created July 21, 2012 03:18
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save adamjmurray/3154437 to your computer and use it in GitHub Desktop.
Save adamjmurray/3154437 to your computer and use it in GitHub Desktop.
Managing Ruby gems programmatically
# Environment, set GEM_HOME & GEM_PATH. For example, we can launch JRuby like this:
# GEM_HOME=/Users/amurray/tmp/gems/ GEM_PATH=/Users/amurray/tmp/gems java -jar ~/Downloads/jruby-complete-1.7.0.preview1.jar -S irb
# =====================
# LISTING gems
puts Gem::Specification.find_all.to_s
puts Gem::Specification.find_all.map{|spec| "#{spec.name} (#{spec.version})" }
# =====================
# USING (a specific version of) gems
gem 'rake', '0.9.0'
require 'rake'
# ====================
# INSTALLING gems
require 'rubygems/commands/install_command'
cmd = Gem::Commands::InstallCommand.new
cmd.handle_options ["--no-ri", "--no-rdoc", 'rake', '--version', '0.9'] # or omit --version and the following option to install the latest
begin
cmd.execute
rescue Gem::SystemExitException => e
puts "DONE: #{e.exit_code}"
end
# =====================
# UNINSTALLING gems
require 'rubygems/commands/uninstall_command'
cmd = Gem::Commands::UninstallCommand.new
cmd.handle_options ['-x', '-I', 'jasmine'] # -x removes executables without prompting. -I ignores dependecies. Can also uninstall specific versions...
cmd.execute
# =====================
# UPDATING a gem to latest version
require 'rubygems/commands/update_command'
cmd = Gem::Commands::UpdateCommand.new
cmd.handle_options ['--no-rdoc', '--no-ri', 'rake']
cmd.execute
@museadmin
Copy link

Really helpful, just what I was looking for.

Thanks for posting this 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment