From: Josh Cheek Date: 2011-05-28T21:39:27+09:00 Subject: Re: Unexpected behavior when referencing nested classes --e0cb4e88758f1f2df404a45558a6 Content-Type: text/plain; charset=ISO-8859-1 On Sat, May 28, 2011 at 7:21 AM, John Feminella wrote: > It was my understanding that consts are first searched for in their > own module scope before Ruby starts looking elsewhere. So, if you have > a class called X in a module M1::M2 and another one at top level, and > you're inside M1::M2, then referencing X gets you M1::M2::X. > > This doesn't appear to be the case though: > > ruby-1.9.2-p0 :001 > module Foo; end > => nil > ruby-1.9.2-p0 :002 > module Foo::Bar; class Baz; end; end > => nil > ruby-1.9.2-p0 :003 > class Baz; def say; "::Baz"; end; end > => nil > ruby-1.9.2-p0 :004 > class Foo::Bar::Baz; def say; "::Foo::Bar::Baz"; > end; def x; Baz.new.say; end; end > => nil > > # expected "Foo::Bar::Baz" > ruby-1.9.2-p0 :005 > Foo::Bar::Baz.new.x > => "::Baz" > > Have I misunderstood how constants work? > > ~ jf > -- > John Feminella > Principal Consultant, BitsBuilder > LI: http://www.linkedin.com/in/johnxf > SO: http://stackoverflow.com/users/75170/ > > I have some vague hypotheses about why it does this, but don't have any idea how to test them. module Foo end module Foo::Bar class Baz end end class Baz def say "::Baz" end end # gives the unexpected result class Foo::Bar::Baz def say "::Foo::Bar::Baz" end def x # !> previous definition of x was here Baz.new.say end end Foo::Bar::Baz.new.x # => "::Baz" # gives the expected result module Foo::Bar class Baz def x # !> method redefined; discarding old x Baz.new.say end end end Foo::Bar::Baz.new.x # => "::Foo::Bar::Baz" --e0cb4e88758f1f2df404a45558a6--