From: Anurag Priyam Date: 2011-05-29T00:30:50+09:00 Subject: Re: Unexpected behavior when referencing nested classes > Have I misunderstood how constants work? Yeah. Ruby first tries to find the constant in the "lexical scope" of the reference - immediately enclosing module/class, then the next enclosing module/class, and so on. You can find out the modules/classes that are searched by calling Module.nesting method at that point. > class Foo::Bar::Baz; def say; "::Foo::Bar::Baz"; > end; def x; Baz.new.say; end; end > Foo::Bar::Baz.new.x Here, Baz is first looked up in Foo::Bar::Baz, and then the global namespace; confirm with Module.nesting. Ruby finds Baz in the later, so '::Baz is the expected output. module Foo::Bar class Baz def say; "::Foo::Bar::Baz"; end def x; Baz.new.say; end end end Foo::Bar::Baz.new.x #=> "::Foo::Bar::Baz" Here, Ruby first looks up Baz in Foo::Bar::Baz, followed by Foo::Bar finds, and then the global namespace. Again, you can confirm this with Module.nesting. Sometime back, I had answered a similar question. You might want to have a look[1]. [1]: http://groups.google.com/group/ruby-talk-google/browse_thread/thread/5f3066bf9366e44f# -- Anurag Priyam http://about.me/yeban/