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]

Gem::Specification.new do |s|
 s.post_install_message  = "Enjoy my gem!"
end

or "another example":http://github.com/tablatom/rubydoctest/blob/master/rubydoctest.gemspec

How to set the main page for rdocs of a gem[edit | edit source]

Gem::Specification.new do |s|
  ...
  s.rdoc_options = ["--title", "EventMachine", "--main", "README", "--line-numbers"]
  s.extra_rdoc_files = ["README", "RELEASE_NOTES", "COPYING", "GNU", "LEGAL", "TODO"]
end

or "another example":http://github.com/eventmachine/evma_httpserver/blob/master/eventmachine_httpserver.gemspec

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' 
  begin
    Gem::Command.build_args = ARGV
    rescue NoMethodError
  end 
  inst = Gem::DependencyInstaller.new
  begin
    if RUBY_VERSION < "1.9"
      inst.install "ruby-debug-base", "~> 0.10.3"
    else
      inst.install "ruby-debug-base19", "~> 0.11.24"
    end
    rescue
      exit(1)
  end 

  f = File.open(File.join(File.dirname(__FILE__), "Rakefile"), "w")   # create dummy rakefile to indicate success
  f.write("task :default\n")
  f.close

http://groups.google.com/group/ruby-talk-google/browse_thread/thread/6a03451af53fb853/5d402de5b0da1adf?lnk=raot&pli=1 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'"
end

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 http://github.com/qrush/gemcutter/blob/master/gem/lib/gemcutter.rb lets you "push" your gem to gemcutter after it's ready

graph gem http://blog.zenspider.com/2009/04/rubygems-now-has-plugins.html 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 http://blog.segment7.net/articles/2009/08/19/rubygems-isit19-1-0 gem