Ruby Programming/RubyGems

From Wikibooks, open books for an open world
Jump to navigation Jump to search

RubyGems[edit | edit source]

Rubygems is a packaging and "download" system for ruby libraries. Using it is typically as easy as

$ gem install xxx

where xxx is some gem name you'd like to install.

More information about rubygems can be had here.

How to display text to the end user after a gem is installed.[edit | edit source] do |s|
 s.post_install_message  = "Enjoy my gem!"

or "another example":

How to set the main page for rdocs of a gem[edit | edit source] do |s|
  s.rdoc_options = ["--title", "EventMachine", "--main", "README", "--line-numbers"]
  s.extra_rdoc_files = ["README", "RELEASE_NOTES", "COPYING", "GNU", "LEGAL", "TODO"]

or "another example":

How to install different versions of gems depending on which version of ruby the installee is using[edit | edit source]

Here's an example: create file ext/mkrf_conf.rb

  require 'rubygems'
  require 'rubygems/command.rb'
  require 'rubygems/dependency_installer.rb' 
    Gem::Command.build_args = ARGV
    rescue NoMethodError
  inst =
    if RUBY_VERSION < "1.9"
      inst.install "ruby-debug-base", "~> 0.10.3"
      inst.install "ruby-debug-base19", "~> 0.11.24"

  f =, "Rakefile"), "w")   # create dummy rakefile to indicate success
  f.write("task :default\n")
  f.close has a description

Also add @gemspec.extensions = 'ext/mkrf_conf.rb'@ to your gemspec.

How to install a gem programmatically[edit | edit source]

see above.

How to run configure from within an extconf.rb[edit | edit source]

Here's an example, from rice's extconf.rb

require 'rbconfig'
require 'rubygems'
require 'ruby/lib/version.rb'

gem_name = "rice-#{Rice::VERSION}"
prefix_dir = File.join(Gem.default_dir, "gems", gem_name, "ruby", "lib")
with_ruby = File.join(Config::CONFIG["bindir"], Config::CONFIG["RUBY_INSTALL_NAME"])

other_opts = ""
env = ""

if RUBY_PLATFORM =~ /darwin10/
  other_opts = "--disable-dependency-tracking"
  env = "ARCHFLAGS='-arch x86_64'"

system "#{env} ./configure --with-ruby=#{with_ruby} --prefix=#{prefix_dir} #{other_opts}"

How to use a Rakefile instead of a Makefile[edit | edit source]

By default if you list

gemspec.extensions = 'ext/extconf.rb'

it will expect a Makefile to have been created, and run Make against it, at install time. You can also list @gemspec.extensions = 'ext/mkrf_conf.rb'@ and it will run that, expect a Rakefile to have been created, and run rake against it.

Plugins[edit | edit source]

Rubygems now supports plugins, for adding new gem xxx commands, or executing on install and uninstall of gems.

Known rubygems plugins[edit | edit source]

Here's a list of plugins:

gemcutter gem lets you "push" your gem to gemcutter after it's ready

graph gem creates a graphviz of your local dependencies

yard gem has a way to create yard docu after installing a gem (instead of the default rdoc).

gem_file_conflict_checker gem: warns you when you install two gems whose lib files collide (which can cause serious problems, and is common for gem developers who install their own versions of gems, etc.)

isit19 gem