Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get a ruby gem into the include path for "require"

Tags:

ruby

rubygems

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

like image 950
Joe Soul-bringer Avatar asked Mar 26 '09 06:03

Joe Soul-bringer


People also ask

Where do RubyGems get installed?

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 ~/.

How does Ruby require work?

Require reads the file from the file system, parses it, saves to the memory, and runs it in a given place. In require, if you modify the specified file when the script is running, those modifications won't be applied, Ruby will use the file from memory, not from the file system of the machine.

What is require in Gemfile?

Gemfiles. Gemfiles require at least one gem source, in the form of the URL for a RubyGems server. Generate a Gemfile with the default rubygems.org source by running bundle init . If you can, use https so your connection to the rubygems.org server will be verified with SSL.


1 Answers

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.

like image 200
rampion Avatar answered Nov 11 '22 22:11

rampion