views:

255

answers:

4

I have a small application which I created using jeweler. The jeweler generates a lib/ directory, where I suppose to write my code.

This gem I'm creating depends on the httparty gem, so, in my Rakefile I put

  Jeweler::Tasks.new do |gem|
    gem.add_dependency('httparty', '>= 0.4.5')
    ...
  end

in my implementation file I put

require 'httparty'

but when I executes it I get:

lib/my_app.rb:1:in `require': no such file to load -- httparty (LoadError)

I have already installed the httparty gem using

sudo gem install httparty

What is missing?

+1  A: 

You need to require rubygems

require 'rubygems'

before you require httparty

khelll
+2  A: 

You need to require rubygems before to require any gem.

require 'rubygems'
require 'httparty'
Damien MATHIEU
Perfect! Thanks
Daniel Cukier
This cures only the symptom not the illnes.
johannes
A: 

Some people do not consider it good practise to require rubygems in distributable code. The solution is to require rubygems before loading your own new gem from your application code. (you'll need to do that anyway)

gerrit
A: 

If you do

require "httparty"

ruby is searching in different locations for httparty.rb or httparty.so. Where ruby searches and in which order is stored in the global variable $:

On my debian system it looks like this:

$: # =>  ["/usr/local/lib/site_ruby/1.8", "/usr/local/lib/site_ruby/1.8/x86_64-linux", "/usr/local/lib/site_ruby", "/usr/lib/ruby/vendor_ruby/1.8", "/usr/lib/ruby/vendor_ruby/1.8/x86_64-linux", "/usr/lib/ruby/vendor_ruby", "/usr/lib/ruby/1.8", "/usr/lib/ruby/1.8/x86_64-linux", "."]

But httparty.rb is in /var/lib/gems/1.8/gems/httparty-0.4.5/lib/httparty.rb, so it can not be found. When you do

require "rubygems"

The Kernel#require method is changed, to find rubygems.

require "rubygems" makes your code platform dependant:

  • Somebody might install your library through another method than rubygems
  • Some linux distributions (ex. gentoo) make it uneccasary to require "rubygems"

On my debian systems I symlink every rubygems library to /usr/local/lib/site_ruby/1.8/, this path is included in the standard search path($:). You can find more about this at http://tomayko.com/writings/require-rubygems-antipattern.

johannes