Question

I am trying to run the following ruby code from IRB but am unable to require the gmail gem.

require 'rubygems'
require 'gmail'

gmail = Gmail.new("user", "pass")

Here's the IRB output:

Johnny-Goodmans-MacBook-Pro:gmail johnnygoodman$ irb
>> require 'rubygems'
=> false
>> require 'gmail'
NameError: uninitialized constant Gmail
        from ./gmail.rb:6
        from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
        from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require'
        from (irb):2

Here is Gem.path output:

>> Gem.path
=> ["/Users/johnnygoodman/.gem/ruby/1.8", "/Library/Ruby/Gems/1.8", "/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8"]

And environment output:

Johnny-Goodmans-MacBook-Pro:gmail johnnygoodman$ gem environment
RubyGems Environment:
  - RUBYGEMS VERSION: 1.3.7
  - RUBY VERSION: 1.8.7 (2009-06-08 patchlevel 173) [universal-darwin10.0]
  - INSTALLATION DIRECTORY: /Library/Ruby/Gems/1.8
  - RUBY EXECUTABLE: /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
  - EXECUTABLE DIRECTORY: /usr/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - universal-darwin-10
  - GEM PATHS:
     - /Library/Ruby/Gems/1.8
     - /Users/johnnygoodman/.gem/ruby/1.8
     - /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
     - :sources => ["http://rubygems.org/", "http://gems.github.com"]
  - REMOTE SOURCES:
     - http://rubygems.org/
     - http://gems.github.com

And which ruby:

Johnny-Goodmans-MacBook-Pro:gmail johnnygoodman$ which ruby
/usr/bin/ruby

And .gemrc:

Johnny-Goodmans-MacBook-Pro:~ johnnygoodman$ cat .gemrc
--- 
:sources: 
- http://rubygems.org/
- http://gems.github.com
:backtrace: false
:benchmark: false
:verbose: true
:update_sources: true
:bulk_threshold: 1000

Here's my gemlist:

Johnny-Goodmans-MacBook-Pro:~ johnnygoodman$ gem list

*** LOCAL GEMS ***

actionmailer (2.3.8, 2.3.5, 2.2.2, 1.3.6)
actionpack (2.3.8, 2.3.5, 2.2.2, 1.13.6)
actionwebservice (1.2.6)
activerecord (2.3.8, 2.3.5, 2.2.2, 1.15.6)
activeresource (2.3.8, 2.3.5, 2.2.2)
activesupport (2.3.8, 2.3.5, 2.2.2, 1.4.4)
acts_as_ferret (0.4.4, 0.4.3)
Ascii85 (1.0.0)
autotest (4.2.10, 4.2.9)
autotest-fsevent (0.2.2, 0.1.1)
autotest-growl (0.2.4, 0.2.0)
autotest-rails (4.1.0)
bigdecimal-segfault-fix (1.0.1)
capistrano (2.5.18, 2.5.11, 2.5.2)
cgi_multipart_eof_fix (2.5.0)
cheat (1.2.1)
chronic (0.2.3)
configuration (1.1.0)
daemons (1.0.10)
dnssd (1.3.1, 0.6.0)
fastthread (1.0.7, 1.0.1)
fcgi (0.8.8, 0.8.7)
ferret (0.11.6)
gem_plugin (0.2.3)
gemcutter (0.5.0, 0.4.1)
heroku (1.9.9, 1.8.5)
highline (1.5.2, 1.5.1, 1.5.0)
hoe (2.6.0, 2.5.0)
hpricot (0.8.2, 0.6.164)
json_pure (1.4.3, 1.2.3, 1.2.2)
launchy (0.3.5)
libxml-ruby (1.1.4, 1.1.3, 1.1.2)
mail (2.2.1)
mechanize (1.0.0, 0.9.3)
mime-types (1.16)
mongrel (1.1.5)
mysql (2.8.1)
needle (1.3.0)
net-scp (1.0.2, 1.0.1)
net-sftp (2.0.4, 2.0.1, 1.1.1)
net-ssh (2.0.22, 2.0.21, 2.0.17, 2.0.4, 1.1.4)
net-ssh-gateway (1.0.1, 1.0.0)
nokogiri (1.4.2, 1.4.1)
passenger (2.2.11, 2.2.9)
pdf-reader (0.8.5, 0.8.4, 0.8.3)
polyglot (0.3.1)
pony (1.0, 0.9, 0.6)
rack (1.1.0, 1.0.1)
rails (2.3.8, 2.3.5, 2.2.2, 1.2.6)
rake (0.8.7, 0.8.3)
RedCloth (4.2.3, 4.1.1)
rest-client (1.4.2, 1.3.1)
rspec (1.3.0)
rspec-rails (1.3.2)
ruby-gmail (0.2.1, 0.0.8)
ruby-openid (2.1.7, 2.1.2)
ruby-yadis (0.3.4)
rubyforge (2.0.4)
rubygems-update (1.3.7, 1.3.6, 1.3.5, 1.3.1)
rubynode (0.1.5)
scrapi (1.2.0)
shared-mime-info (0.1)
sqlite3-ruby (1.2.5, 1.2.4)
sys-uname (0.8.4)
termios (0.9.4)
tidy (1.1.2)
tmail (1.2.7.1)
treetop (1.4.5)
xmpp4r (0.5, 0.4)
ZenTest (4.3.1)

I've uninstalled mac ports and used hivelogic instructions to install a fresh ruby/rails version. However, notice that my which ruby doesn't match hivelogic's instructions until I run:

Johnny-Goodmans-MacBook-Pro:~ johnnygoodman$ . ~/.profile
Johnny-Goodmans-MacBook-Pro:~ johnnygoodman$ which ruby
/usr/local/bin/ruby

I think this is the problem but am not sure what to read to force the ruby path to always load to /usr/local/bin/ruby. It could also be a red herring as I simply don't know how the parts fit. As soon as sudo gem install doesn't work, I'm basically lost.

Note other gems do work:

Johnny-Goodmans-MacBook-Pro:~ johnnygoodman$ irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'mechanize'
=> true

Questions:

  1. When sudo gem install doesn't allow you to create an object from the class, what are the next steps in sleuthing down the issue?

  2. How do the parts of a gem fit together as applies to fixing these types of issues? I've read the FAQs and docs (http://docs.rubygems.org/read/book/3) but either missed it or was unable to pick out the relevant parts.

Was it helpful?

Solution

Looks like you have some misunderstanding how ruby loads files. There's an include path (check out the $: variable). When you require something, ruby will start at the first path in the array, and checks if the file is there. If it's, then it loads it, otherwise it moves on to the next directory in the include path.

When you require gmail, it looks like that it loads your own gmail.rb. Doing a require "gmail" in that file will find itself, instead of the gem's gmail.rb.

To fix it you should probably rename your gmail.rb to something else (my_gmail.rb, or anything, just make sure it doesn't match any system file).

(The require 'ruby-gmail' fails because the ruby-gmail gem doesn't contain such file. You should use simply require 'gmail', but before that rename your gmail.rb)

OTHER TIPS

Your IRB output seems to be about as far as you needed to go:

>> require 'gmail'
NameError: uninitialized constant Gmail
        from ./gmail.rb:6

When you require 'gmail' it's pulling in your local gmail.rb file - within which there's a reference to a Gmail class(?) which isn't defined.

It's unclear why you'd expect Gmail to be defined, though. Without seeing the code in gmail.rb, it's hard to say what's wrong.

I notice that in your gem list there isn't any gem called "gmail" or anything similar.

It's hard to tell, therefore, what you're trying to accomplish. Perhaps you're confusing gems with a basic library require, or are assuming that the Rails constant-lookup functionality is a Ruby feature.

Most useful would be if you could post the contents of gmail.rb.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top