Require_gem scoping issue?

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

On 11/6/06, Pawel S. [email protected] 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://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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs