# Const\_missing - why is it necessary to hook it at Module?

**URL:** <https://www.ruby-forum.com/t/const-missing-why-is-it-necessary-to-hook-it-at-module/89181>\
**Category:** Ruby\
**Created:** [March 18, 2007, 8:00am UTC](https://www.ruby-forum.com/t/const-missing-why-is-it-necessary-to-hook-it-at-module/89181 "2007-03-18T08:00:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Clifford\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/49beb7/32.png) [@Clifford\_H](https://www.ruby-forum.com/u/Clifford_H)\
**Post date:** [March 18, 2007, 8:00am UTC](https://www.ruby-forum.com/t/const-missing-why-is-it-necessary-to-hook-it-at-module/89181/1 "2007-03-18T08:00:46Z")

</div>

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

Clifford H…

---

<div class="post-metadata">

**Author:** ![Clifford\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/49beb7/32.png) [@Clifford\_H](https://www.ruby-forum.com/u/Clifford_H)\
**Post date:** [March 18, 2007, 1:15pm UTC](https://www.ruby-forum.com/t/const-missing-why-is-it-necessary-to-hook-it-at-module/89181/2 "2007-03-18T13:15:10Z")

</div>

Hi –

On 3/18/07, Clifford H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> 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

---

<div class="post-metadata">

**Author:** ![Clifford\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/49beb7/32.png) [@Clifford\_H](https://www.ruby-forum.com/u/Clifford_H)\
**Post date:** [March 19, 2007, 1:01am UTC](https://www.ruby-forum.com/t/const-missing-why-is-it-necessary-to-hook-it-at-module/89181/3 "2007-03-19T01:01:35Z")

</div>

David A. Black wrote:

> Constants don’t depend on self for their scope; they use a kind of  
> quasi-static scoping

Ok, thanks David, I figured it was something like that.  
There are very few cases where const\_missing gets used  
(Rails and Rake being two), and it’s not obvious from  
them or from the documentation that it works like that.

BTW, I recently saw a video of your “Curious developer”  
talk on database design at the RailsConf 2006. I expect  
to address many of the issues through my “ActiveFacts”  
project (this DSL I’m working on is a fact-based data  
modeling language). Would you be interested in private  
communication regarding this project?

Clifford H…

---

<div class="post-metadata">

**Author:** ![Clifford\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/49beb7/32.png) [@Clifford\_H](https://www.ruby-forum.com/u/Clifford_H)\
**Post date:** [March 20, 2007, 4:21am UTC](https://www.ruby-forum.com/t/const-missing-why-is-it-necessary-to-hook-it-at-module/89181/4 "2007-03-20T04:21:48Z")

</div>

Clifford H. wrote:

> Would you be interested in private  
> communication regarding this project?

David, I got your response. Perhaps you didn’t see mine?

Clifford H…
