require_gem scoping issue?

Discussion in 'Ruby' started by Pawel Szymczykowski, Nov 7, 2006.

  1. 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
     
    Pawel Szymczykowski, Nov 7, 2006
    #1
    1. Advertising

  2. On 11/6/06, Pawel Szymczykowski <> 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/cre...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
     
    Pawel Szymczykowski, Nov 7, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gavin Sinclair

    require vs require_gem

    Gavin Sinclair, Jun 17, 2004, in forum: Ruby
    Replies:
    2
    Views:
    140
    Chad Fowler
    Jun 18, 2004
  2. Eirikur Hallgrimsson
    Replies:
    3
    Views:
    175
    Gavin Sinclair
    Oct 10, 2004
  3. Lars Olsson
    Replies:
    2
    Views:
    112
    Jim Weirich
    Dec 2, 2005
  4. Wes Gamble
    Replies:
    3
    Views:
    135
    Jim Weirich
    Mar 23, 2006
  5. Andrew Gibson

    require_gem question

    Andrew Gibson, Jul 26, 2006, in forum: Ruby
    Replies:
    1
    Views:
    104
    Eric Hodel
    Jul 29, 2006
Loading...

Share This Page