From: "David A. Black" Date: 2007-03-18T21:14:12+09:00 Subject: Re: const_missing - why is it necessary to hook it at Module? Hi -- On 3/18/07, Clifford Heath wrote: > Why doesn't the following code work? > > class Foo > def Foo.const_missing(sym) > p sym > sym.to_s > end > end > > Foo.new.instance_eval { > Bar > } > > I thought the missing constant Bar would be caught > by the const_missing, since it's available on the > class whose instance is being instance_eval'd. > > What gives? I'm writing a DSL and want to catch missing > constants within a block being instance_eval'd All that instance_eval does is set self and execute the block. Constants don't depend on self for their scope; they use a kind of quasi-static scoping, and the Bar in your block is resolved (unsuccessfully) as Bar from the top level. You'd need to add your const_missing method to Object, so as to cover the top level. David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com)