2

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?

6 Answers 6

8

You need to require rubygems before requiring any gem.

require 'rubygems' require 'httparty' 
Sign up to request clarification or add additional context in comments.

1 Comment

This cures only the symptom not the illnes.
1

You need to require rubygems

require 'rubygems' 

before you require httparty

Comments

1

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.

Comments

0

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)

Comments

0

I had the same problem with line:

gem "httparty", :git => 'https://github.com/jnunemaker/httparty.git' 

When I turn to:

gem "httparty" 

everything started to work.

Comments

0

This error can occur when you try to make http posts with Ruby, but have not completed setting up Rubygems and RVM.

  1. First, check that rubygems and rubygems-update are installed properly using the tutorial here:http://docs.rubygems.org/read/chapter/3

  2. If you're using RVM, you'll also need to create a gemset and select a ruby version first.

$ rvm gemset list

If this shows you're on (default), then you'll need to configure your RVM.

Find the ruby version you want to use ($ ruby -v) will work and substitute it here:

$ rvm use 1.9.2-head@albinochipmunk --create

  1. After your rubygems is installed and your RVM has a selected gemset and ruby version, you'll be able to install httparty without sudo:

$ gem install httparty

Installing httparty with sudo may break RVM configuration, so you want to avoid that. Also, using a new RVM gemset will mean that you may need to reinstall some gems.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.