# Require\_gem scoping issue?

**URL:** <https://www.ruby-forum.com/t/require-gem-scoping-issue/75057>\
**Category:** Ruby\
**Created:** [November 7, 2006, 11:08am UTC](https://www.ruby-forum.com/t/require-gem-scoping-issue/75057 "2006-11-07T11:08:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![makenai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8797f3/32.png) [@makenai](https://www.ruby-forum.com/u/makenai)\
**Post date:** [November 7, 2006, 11:08am UTC](https://www.ruby-forum.com/t/require-gem-scoping-issue/75057/1 "2006-11-07T11:08:48Z")

</div>

Hi all,

I’m running into a weird problem with rubygems and using  
‘require\_gem’. Given the following code,

– being code –

require ‘rubygems’  
require\_gem ‘mechanize’, ‘\>= 0.6.3’

class MyClass

```
def initialize
  @agent = WWW::Mechanize.new # Line 7
end

```

end

mc = MyClass.new # Line 12

– end code –

I get this error:

NameError: uninitialized constant MyClass::WWW

method initialize in untitled document at line 7  
at top level in untitled document at line 12

Could it be that I’m missing something really obvious? When I replace  
the require\_gem line with just “require ‘mechanize’”, it works  
according to my expectations. I’m using ruby 1.8.4, rubygems 0.9.0  
(also tried with 0.8.11) and mechanize is at 0.6.3 as a gem. It  
doesn’t seem to matter if the explicit version is there or not, the  
same error is produced.

Thanks!

-Pawel

---

<div class="post-metadata">

**Author:** ![makenai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8797f3/32.png) [@makenai](https://www.ruby-forum.com/u/makenai)\
**Post date:** [November 7, 2006, 11:08am UTC](https://www.ruby-forum.com/t/require-gem-scoping-issue/75057/2 "2006-11-07T11:08:50Z")

</div>

On 11/6/06, Pawel S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi all,
> 
> I’m running into a weird problem with rubygems and using  
> ‘require\_gem’. Given the following code,

Hm… nevermind. I’m a fool. A foolish fool. I just found the following  
pages that explains the issue:

[http://redhanded.hobix.com/inspect/autorequireIsBasicallyGoneEveryone.html](http://redhanded.hobix.com/inspect/autorequireIsBasicallyGoneEveryone.html)  
[http://desperatepundit.com/blog/cremes/technology/2006/05/30/Ruby-Tip-1-require-gem-is-unnecessary.html](http://desperatepundit.com/blog/cremes/technology/2006/05/30/Ruby-Tip-1-require-gem-is-unnecessary.html)

So it looks like I have to do:

require\_gem ‘mechanize’, ‘\>= 0.6.3’  
require ‘mechanize’

In order to require and load a specific version of a gem. Oh well.

-Pawel
