Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby thinks I'm referencing a top-level constant even when I specify the full namespace

Tags:

ruby

In my app I have

class User
  include User::Foo
end

User::Foo is defined in app/models/user/foo.rb

Now I'm using a library that defines its own Foo class. I'm getting this error:

warning: toplevel constant Foo referenced by User::Foo

User only refers to Foo with the full path, User::Foo, and Foo never actually refers to Foo.

What's going on?

update: just remembered I had the same problem before, seen here in question 1: How do I refer to a submodule's "full path" in ruby?

like image 497
John Bachir Avatar asked Jun 01 '11 21:06

John Bachir


1 Answers

Make sure the User::Foo definition you want is visible

The message says: the only definition it found was at the top level. This is obviously suspicious since you went to the trouble of qualifying the name.

There isn't a good way to say you want a different ::User. It's a class and ruby will look for a definition there, then at the top level. You need to somehow specify the module without referencing your class.

One way to fix this:

module Other
  class User
    include ::User::Foo
  end
end
like image 77
DigitalRoss Avatar answered Nov 23 '22 02:11

DigitalRoss