tags:

views:

2854

answers:

3

Hi all,

I am creating what I expect to be ruby gem. Anyone have a good link to a tutorial on converting a simple library or plugin to a gem? Also, especially, what is the process that Ruby uses to allow the require to find gems? It seems to be something more than putting the files in the gem path (or is my configuration screwed up?).

Thanks

+2  A: 

There's an oldish one here from Dr Nic, which uses newgem. That is probably a bit out of date now.

Somewhat more recent, we get gemify, which seems to build on the above. Here's a post about that

As far as paths go, once you've executed gem install <some-useful-stuff> and your requiring code has either executed require 'rubygems' or you've set RUBYOPT=-rubygems (is that Windows only? I'm not sure) then subsequent executions of require will also search the gems directory for an appropriate library.

There's also the more explicit (but deprecated) require_gem command, which is now just plain gem, which allows you to specify version numbers.

Mike Woodhouse
+1  A: 

For building a ruby gem you may want to look at newgem or jeweler.

dylanfm
+7  A: 

It's actually not to hard to do this manually. Let's say you have a library whatever.rb that you want to distribute as a gem.

  1. make a directory lib and put a copy of whatever.rb in lib/whatever.rb.
  2. make a file whatever.gemspec, and put the following in there, filling in the appropriate values:
    
     Gem::Specification.new do |spec|
       spec.name = 'the-name-of-your-gem'
       spec.version ='0.0.1'
    # this is important - it specifies which files to include in the gem. spec.files = ["lib/whatever.rb"]
    # optional, but useful to your users spec.summary = "A more longwinded description of your gem" spec.author = 'Your Name' spec.email = '[email protected]' spec.homepage = 'http://www.yourpage.com'
    # you did document with RDoc, right? spec.has_rdoc = true
    # if you have a ruby forge project spec.rubyforge_project = 'your-project-name-on-rubyforge'
    # if you have any dependencies on other gems, list them thusly spec.add_dependency('hpricot') spec.add_dependency('log4r', '>= 1.0.5') end
  3. now, to build the gem, use the gem build command:
    % gem build whatever.gemspec
    Successfully built RubyGem
    Name: the-name-of-your-gem
    Version: 0.0.1
    File: the-name-of-your-gem-0.0.1.gem
    %
    
  4. You can test locally by using gem install the-name-of-your-gem-0.0.1.gem To use your library in a script then, simply do the following at the top:
    
    require 'rubygems' # puts gem libraries in the require path
    require 'whatever' # loads your library
    

For more on what the various settings in the gemspec file, check the GemSpec Reference.

Personally, I use rubygems a lot to package executable scripts as well, and find it very handy for that.

rampion
Did someone mark down this fine answer, detailed answer without even a comment?
Joe Soul-bringer