O'Reilly logo

Ruby Cookbook by Leonard Richardson, Lucas Carlson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

18.6. Packaging Your Code as a Gem

Problem

You want to package a program you wrote as a Ruby gem, possibly to distribute it on the main gem server at rubyforge.org.

Solution

First, you must write a specification file. This file consists of a few lines of Ruby code that instantiate a Gem::Specification object and populate it with information about your program. Assuming that all of your program's files are in a subdirectory called lib/, the following might make a good specification file:

	# shielding.gemspec
	require 'rubygems'
	spec = Gem::Specification.new do |spec|
	  spec.name = 'shielding'
	  spec.summary = 'A library for calculating the strength of duophasic shielding'
	  spec.description = %{This library calculates to high precision the
	  physical and electrostatic strength of a duophasic shield. It knows
	  about most real-world shield configurations, as well as many
	  theoretical arrangements not yet built.}
	  spec.author = 'Bob Zaff'
	  spec.email = 'zaff@example.com'
	  spec.homepage = 'http://www.example.com/software/shielding/'
	  spec.files = Dir['lib/*.rb']
	  spec.version = '1.0.0'
	end

You can then use the gem build command to create the actual gem from its specification file:

	$ gem build shielding.gemspec
	Attempting to build gem spec 'shielding.gemspec'
	  Successfully built RubyGem
	  Name: shielding
	  Version: 1.0.0
	  File: shielding-1.0.0.gem

	$ ls
	shield.gemspec shielding-1.0.0.gem

Then install the gem normally:

 $ gem install ./shielding-1.0.0.gem Attempting local installation of './shielding-1.0.0.gem' Successfully ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required