# Dynamic finding of missing constants

**URL:** <https://rubytalk.org/t/dynamic-finding-of-missing-constants/22775>\
**Category:** ruby-talk\
**Created:** [25 November 2005 16:22 UTC](https://rubytalk.org/t/dynamic-finding-of-missing-constants/22775 "2005-11-25T16:22:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ola\_Bini3](https://avatars.discourse-cdn.com/v4/letter/o/85e7bf/32.png) [@Ola\_Bini3](https://rubytalk.org/u/Ola_Bini3)\
**Post date:** [25 November 2005 16:22 UTC](https://rubytalk.org/t/dynamic-finding-of-missing-constants/22775/1 "2005-11-25T16:22:08Z")

</div>

Hi.

I'm doing some slightly interesting things with Ruby, and I wanted to know of there's a language way of interrupting the process for finding a Constant name, like the method\_missing method. The closest thing I've been able to work out is to rescue the NameError at some toplevel, but this is not transparent enough for me. Anyone have good any good suggestions?

Regards  
&nbsp;&nbsp;Ola Bini

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [25 November 2005 16:28 UTC](https://rubytalk.org/t/dynamic-finding-of-missing-constants/22775/2 "2005-11-25T16:28:07Z")

</div>

Yep, const\_missing().

James Edward Gray II

> **···**
>
> On Nov 25, 2005, at 10:22 AM, Ola Bini wrote:
> 
> > Hi.
> > 
> > I'm doing some slightly interesting things with Ruby, and I wanted to know of there's a language way of interrupting the process for finding a Constant name, like the method\_missing method.

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [25 November 2005 16:28 UTC](https://rubytalk.org/t/dynamic-finding-of-missing-constants/22775/3 "2005-11-25T16:28:28Z")

</div>

> I'm doing some slightly interesting things with Ruby, and I wanted to know  
> of there's a language way of interrupting the process for finding a  
> Constant name, like the method\_missing method.

You have Module#const\_missing

moulon% ri Module#const\_missing  
--------------------------------------------------- Module#const\_missing  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mod.const\_missing(sym) =\> obj

> **···**
>
> ------------------------------------------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Invoked when a reference is made to an undefined constant in \_mod\_.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;It is passed a symbol for the undefined constant, and returns a  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value to be used for that constant. The following code is a (very  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bad) example: if reference is made to an undefined constant, it  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attempts to load a file whose name is the lowercase version of the  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;constant (thus class +Fred+ is assumed to be in file +fred.rb+). If  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;found, it returns the value of the loaded class. It therefore  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;implements a perverse kind of autoload facility.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def Object.const\_missing(name)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@looked\_for ||= {}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;str\_name = name.to\_s  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise "Class not found: #{name}" if @looked\_for[str\_name]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@looked\_for[str\_name] = 1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file = str\_name.downcase  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require file  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;klass = const\_get(name)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return klass if klass  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise "Class not found: #{name}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
> 
> moulon%
> 
> Guy Decoux

---

<div class="post-metadata">

**Author:** ![Ola\_Bini3](https://avatars.discourse-cdn.com/v4/letter/o/85e7bf/32.png) [@Ola\_Bini3](https://rubytalk.org/u/Ola_Bini3)\
**Post date:** [25 November 2005 16:31 UTC](https://rubytalk.org/t/dynamic-finding-of-missing-constants/22775/4 "2005-11-25T16:31:26Z")

</div>

Ah, sorry. Thanks for the pointer. I thought I checked for it. But apparently my brain is fried after to much friday-work.

/O

> **···**
>
> At 17:28 2005-11-25, you wrote:
> 
> > On Nov 25, 2005, at 10:22 AM, Ola Bini wrote:
> > 
> > > Hi.
> > > 
> > > I'm doing some slightly interesting things with Ruby, and I wanted  
> > > to know of there's a language way of interrupting the process for  
> > > finding a Constant name, like the method\_missing method.
> > 
> > Yep, const\_missing().
> > 
> > James Edward Gray II
