# Gem and uninitialized constant

**URL:** <https://www.ruby-forum.com/t/gem-and-uninitialized-constant/91264>\
**Category:** Ruby\
**Created:** [April 6, 2007, 3:45am UTC](https://www.ruby-forum.com/t/gem-and-uninitialized-constant/91264 "2007-04-06T03:45:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Franey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@John\_Franey](https://www.ruby-forum.com/u/John_Franey)\
**Post date:** [April 6, 2007, 3:45am UTC](https://www.ruby-forum.com/t/gem-and-uninitialized-constant/91264/1 "2007-04-06T03:45:13Z")

</div>

I want to use NetAddr 1.2 with gems 0.9.2 and ruby 1.8.5 on Suse 10.2.  
The lines of code:

require ‘rubygems’  
gem ‘netaddr’  
ipadd = NetAddr::CIDR.create(“192.9.200.0/24”)

The error: uninitialized constant NetAddr (NameError)

This works:

require ‘rubygems’  
require\_gem ‘netaddr’  
ipadd = NetAddr::CIDR.create(“192.9.200.0/24”)

But I get the warning: “require\_gem is obsolete. Use gem instead.”

Any comments?

Thanks

---

<div class="post-metadata">

**Author:** ![John\_Franey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@John\_Franey](https://www.ruby-forum.com/u/John_Franey)\
**Post date:** [April 6, 2007, 8:06am UTC](https://www.ruby-forum.com/t/gem-and-uninitialized-constant/91264/2 "2007-04-06T08:06:28Z")

</div>

On Fri, Apr 06, 2007 at 10:44:00AM +0900, John Franey wrote:

> This works:
> 
> require ‘rubygems’  
> require\_gem ‘netaddr’  
> ipadd = NetAddr::CIDR.create(“192.9.200.0/24”)
> 
> But I get the warning: “require\_gem is obsolete. Use gem instead.”

Have you tried:

require ‘rubygems’  
require ‘netaddr’

?  
(Newer versions of rubygems replace ‘require’ directly)

---

<div class="post-metadata">

**Author:** ![John\_Franey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@John\_Franey](https://www.ruby-forum.com/u/John_Franey)\
**Post date:** [April 6, 2007, 7:51pm UTC](https://www.ruby-forum.com/t/gem-and-uninitialized-constant/91264/3 "2007-04-06T19:51:06Z")

</div>

Brian,

Thanks for responding:

This code:

require ‘rubygems’  
require ‘netaddr’  
ipadd = NetAddr::CIDR.create(“192.9.200.0/24”)

gives:

/usr/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:27:in  
`gem_original_require': no such file to load -- netaddr (LoadError) from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in`require’  
from ci.rb:2

This code:

require ‘rubygems’  
require ‘net\_addr’  
ipadd = NetAddr::CIDR.create(“192.9.200.0/24”)

works.

Now, how am I supposed to learn the right string to put into the require  
call. The command to install was:

gem install netaddr

I don’t remember why I guessed ‘net\_addr’

John

---

<div class="post-metadata">

**Author:** ![John\_Franey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@John\_Franey](https://www.ruby-forum.com/u/John_Franey)\
**Post date:** [April 6, 2007, 8:51pm UTC](https://www.ruby-forum.com/t/gem-and-uninitialized-constant/91264/4 "2007-04-06T20:51:14Z")

</div>

John J. Franey wrote:

> gives:  
> require ‘net\_addr’  
> ipadd = NetAddr::CIDR.create(“192.9.200.0/24”)
> 
> works.
> 
> Now, how am I supposed to learn the right string to put into the require  
> call. The command to install was:
> 
> gem install netaddr

If the gem generates documentation, I’d look there. Otherwise, look into  
your /usr/lib/ruby/gems/1.8/name\_of\_gem directories for a README or code  
code examples. In case you didn’t know it gem\_server launches a small  
server, which makes the RDoc documentation accessible (in case the  
developer was smart enough to include such a thing 😉

Otherwise, you might take your chances and even look at the gem’s  
homepage. in this case, it probably is [http://netaddr.rubyforge.org](http://netaddr.rubyforge.org)

P.S.:  
A: Because it makes reading replies difficult.  
Q: Why is top-posting bad?

–  
Phillip “CynicalRyan” Gawlowski  
[http://cynicalryan.110mb.com/](http://cynicalryan.110mb.com/)

Rule of Open-Source Programming #1:

Don’t whine unless you are going to implement it yourself.

---

<div class="post-metadata">

**Author:** ![John\_Franey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@John\_Franey](https://www.ruby-forum.com/u/John_Franey)\
**Post date:** [April 7, 2007, 11:30am UTC](https://www.ruby-forum.com/t/gem-and-uninitialized-constant/91264/5 "2007-04-07T11:30:14Z")

</div>

On Sat, Apr 07, 2007 at 02:49:54AM +0900, John J. Franey wrote:

> This code:
> 
> require ‘rubygems’  
> require ‘net\_addr’  
> ipadd = NetAddr::CIDR.create(“192.9.200.0/24”)
> 
> works.
> 
> Now, how am I supposed to learn the right string to put into the require  
> call.

I’d just do  
ls -lR /usr/lib/ruby/gems/1.8/gems/netaddr\*

---

<div class="post-metadata">

**Author:** ![John\_Franey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@John\_Franey](https://www.ruby-forum.com/u/John_Franey)\
**Post date:** [April 7, 2007, 7:20am UTC](https://www.ruby-forum.com/t/gem-and-uninitialized-constant/91264/6 "2007-04-07T07:20:56Z")

</div>

On Apr 6, 2:50 pm, Phillip G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > require ‘rubygems’  
> > If the gem generates documentation, I’d look there. Otherwise, look into  
> > Q: Why is top-posting bad?
> 
> –  
> Phillip “CynicalRyan” Gawlowskihttp://cynicalryan.110mb.com/
> 
> Rule of Open-Source Programming #1:
> 
> Don’t whine unless you are going to implement it yourself.- Hide quoted text -
> 
> - Show quoted text -

I got caught in the same puzzle that the OP got trapped.

A gem has a name “gemname” which is used to install — gem install  
gemname  
A “require” loads the gem and needs to know the gem’s base  
filename … which is the filename without an extension.  
Usually (probably 99% of time), the gemname and the base filename are  
the same although it is not a requirement.  
A gem filename may have “rb” as an extension but it could also have  
“so” as an extension … ‘require’ is smart enough to look  
for valid extensions.

As CynicalRyan stated, you have to dig into the documentation and hope  
to find the information.

In the case of the netaddr gem, the example in the documentation is  
outdated. Only by looking at the list of netaddr’s files would you  
have seen “net\_addr.rb”

---

<div class="post-metadata">

**Author:** ![John\_Franey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@John\_Franey](https://www.ruby-forum.com/u/John_Franey)\
**Post date:** [April 7, 2007, 5:43pm UTC](https://www.ruby-forum.com/t/gem-and-uninitialized-constant/91264/7 "2007-04-07T17:43:59Z")

</div>

bbiker wrote:

> fails:
> 
> > developer was smart enough to include such a thing 😉  
> > I got caught in the same puzzle that the OP got trapped.

I got good responses to this question thanks. Basically, the answer is  
“dig”. I can do and did in this case. Digging won’t be the best way  
after  
someone has already solved the problem with a command, which is what I  
was  
fishing for. Thanks for you help and understanding.

John
