Uninitialized Constant Foo

16 Oct 2009

I created this issue twice this week doing the same thing so perhaps this post will keep me from doing it again.

NameError: uninitialized constant Foo

The first time I had just installed a gem. We'll pretend the gem was called Foo. I wanted to try and couldn't seem to use it. The strange thing was that I could use the gem in irb but not in this script file. Here's my script, lets see if you see the problem right away:

contents of foo.rb:
  require 'rubygems'
  require 'foo'
  job = Foo::Job.new
  puts job.result

It would error on line 4: job = Foo::Job.new Do you see the problem?

It is requiring 'foo' which is the name of the gem library but unfortunately for me and the hairs I pulled out of my head it is also the name of the file (foo.rb).

Renaming the file to foo_playground.rb fixed the issue. HEAD SLAP!