Why doesn’t Ruby find classes in a higher scope when module is specified using ::?

Is this a bug, or is it just a logical consequence

It’s a “quirk”. Some consider it a bug.

Parent scopes used for looking up unresolved constants are determined by module nesting. It just so happens that when you use module Top::Foo, it creates just one level of nesting instead of two. Observe:

module Top
  module Foo
    class SomeTest
      Module.nesting # => [Top::Foo::SomeTest, Top::Foo, Top]
    end
  end
end

module Top::Foo
  class SomeTest
    Module.nesting # => [Top::Foo::SomeTest, Top::Foo]
  end
end

Leave a Comment