From: Logan Capaldo Date: 2006-09-11T23:25:29+09:00 Subject: Re: Constant lookup starting in superclass, not derived class On Mon, Sep 11, 2006 at 09:59:21PM +0900, Chris Roos wrote: > I'm no doubt missing something obvious but I found this a little > confusing. I would have expected the constant lookup to start in Bar > and therefore succeed. Instead, it appears to start in Base where Foo > is not defined. > > class Base > def foo > p Foo > end > end > > class Bar < Base > class Foo > end > end > > Bar.new.foo > # ~> -:3:in `foo': uninitialized constant Base::Foo (NameError) > # ~> from -:12 > > Can anyone explain what is happening please? Constants, unlike methods and instance variables, are "quasi"-lexicaly scoped. (I say "quasi" because a sub class can assign to a constant without munging its parent class's constant) One way of getting around this particular situation is to use #const_get. e.g.: class Base def foo p self.class.const_get('Foo') end end