Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the modern way to structure a ruby gem?

Tags:

ruby

rubygems

Has much changed with the release of Bundler? Is there a template that can be used as a base? What are the best practices?

like image 941
grk Avatar asked Jul 22 '10 08:07

grk


People also ask

What is a Ruby Gem programming?

RubyGems is a package manager for the Ruby programming language that provides a standard format for distributing Ruby programs and libraries (in a self-contained format called a "gem"), a tool designed to easily manage the installation of gems, and a server for distributing them.

What is a Ruby Gem file?

A Gemfile is a file that is created to describe the gem dependencies required to run a Ruby program. A Gemfile should always be placed in the root of the project directory.

Where do I put RubyGems?

When you use the --user-install option, RubyGems will install the gems to a directory inside your home directory, something like ~/. gem/ruby/1.9. 1 . The commands provided by the gems you installed will end up in ~/.


2 Answers

Some posts that I have found useful:

  • http://chneukirchen.github.com/rps/
  • http://tomayko.com/writings/require-rubygems-antipattern
  • http://yehudakatz.com/2009/07/24/rubygems-good-practice/
  • http://weblog.rubyonrails.org/2009/9/1/gem-packaging-best-practices

Edit (2012-01-10): An excellent all-around guide to gem best practices is RubyGems Guides. I would highly recommend starting here now.

To summarize the key points:

  • Use the basic lib/gem.rb and lib/gem/ structure for code.
  • Put any executables in bin, any data files in data and tests in test or spec.
  • Don't require or depend upon files outside of the load path. (VERSION files often seem to live in odd places in gems.)
  • Do not require 'rubygems'.
  • Do not tamper with the $LOAD_PATH.
  • If you find yourself writing require File.join(__FILE__, 'foo', 'bar'), you're doing it wrong.
like image 118
Telemachus Avatar answered Oct 17 '22 22:10

Telemachus


The simplest way it's to use bundler:

bundle gem <gem_name> 

You may even use it in an existing project from the parent directory.

like image 27
jmoreira Avatar answered Oct 17 '22 22:10

jmoreira