# Dynamic finding of missing constants

**URL:** <https://www.ruby-forum.com/t/dynamic-finding-of-missing-constants/48849>\
**Category:** Ruby\
**Created:** [November 25, 2005, 5:23pm UTC](https://www.ruby-forum.com/t/dynamic-finding-of-missing-constants/48849 "2005-11-25T17:23:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ola.Bini](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/7c8e57/32.png) [@Ola.Bini](https://www.ruby-forum.com/u/Ola.Bini)\
**Post date:** [November 25, 2005, 5:23pm UTC](https://www.ruby-forum.com/t/dynamic-finding-of-missing-constants/48849/1 "2005-11-25T17:23:38Z")

</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  
Ola B.

---

<div class="post-metadata">

**Author:** ![Ola.Bini](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/7c8e57/32.png) [@Ola.Bini](https://www.ruby-forum.com/u/Ola.Bini)\
**Post date:** [November 25, 2005, 5:31pm UTC](https://www.ruby-forum.com/t/dynamic-finding-of-missing-constants/48849/2 "2005-11-25T17:31:41Z")

</div>

On Nov 25, 2005, at 10:22 AM, Ola B. 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 G. II

---

<div class="post-metadata">

**Author:** ![Ola.Bini](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/7c8e57/32.png) [@Ola.Bini](https://www.ruby-forum.com/u/Ola.Bini)\
**Post date:** [November 25, 2005, 5:31pm UTC](https://www.ruby-forum.com/t/dynamic-finding-of-missing-constants/48849/3 "2005-11-25T17:31:41Z")

</div>

> > > > > “O” == Ola B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

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

You have Module#const\_missing

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

```
 Invoked when a reference is made to an undefined constant in _mod_.
 It is passed a symbol for the undefined constant, and returns a
 value to be used for that constant. The following code is a (very
 bad) example: if reference is made to an undefined constant, it
 attempts to load a file whose name is the lowercase version of the
 constant (thus class +Fred+ is assumed to be in file +fred.rb+). If
 found, it returns the value of the loaded class. It therefore
 implements a perverse kind of autoload facility.

   def Object.const_missing(name)
     @looked_for ||= {}
     str_name = name.to_s
     raise "Class not found: #{name}" if @looked_for[str_name]
     @looked_for[str_name] = 1
     file = str_name.downcase
     require file
     klass = const_get(name)
     return klass if klass
     raise "Class not found: #{name}"
   end

```

moulon%

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Ola.Bini](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/7c8e57/32.png) [@Ola.Bini](https://www.ruby-forum.com/u/Ola.Bini)\
**Post date:** [November 25, 2005, 5:31pm UTC](https://www.ruby-forum.com/t/dynamic-finding-of-missing-constants/48849/4 "2005-11-25T17:31:41Z")

</div>

At 17:28 2005-11-25, you wrote:

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

/O
