# Class\_eval doesn't find const\_missing

**URL:** <https://www.ruby-forum.com/t/class-eval-doesnt-find-const-missing/206077>\
**Category:** Ruby\
**Created:** [April 21, 2011, 9:20pm UTC](https://www.ruby-forum.com/t/class-eval-doesnt-find-const-missing/206077 "2011-04-21T21:20:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [April 21, 2011, 9:20pm UTC](https://www.ruby-forum.com/t/class-eval-doesnt-find-const-missing/206077/1 "2011-04-21T21:20:44Z")

</div>

I am trying to write a DSL which uses method\_missing and const\_missing  
to catch upper and lowercase names (specified in isolation) and fetch  
the asscoated (already initialized) objects. This works great until I  
need to use class\_eval which seemingly doesn’t follow the custom  
const\_missing method.

Here’s a more simple example of a class with a constant and  
#method\_missing and #const\_missing methods.

class DummyClass

NAME = ‘dummy constant’

def self.method\_missing(method,\*args,&block)  
return “this #{method} was captured by #method\_missing”  
end

def self.const\_missing(constant)  
return “this #{constant} was captured by #const\_missing”  
end

end

Accessing the constant is no problem

> DummyClass::NAME  
> =\> “dummy constant”

Unknown methods and constants are handled as expected

> DummyClass::ANOTHER\_CONSTANT  
> =\> “this ANOTHER\_CONSTANT was captured by #const\_missing”  
> DummyClass.some\_method  
> =\> “this some\_method was captured by #method\_missing”

Accessing the constant via class\_eval is variable - it works when  
evaluating strings but not blocks.

> DummyClass.class\_eval “NAME”  
> =\> “dummy constant”  
> DummyClass.class\_eval {NAME}  
> NameError: uninitialized constant NAME  
> from (irb):14  
> from (irb):14:in `class\_eval’  
> from (irb):14  
> from :0

This is partly understood since blocks are scoped to the context in  
which they were created. But even if the explicit constant is not  
recognised, why isn’t the class #const\_missing method invoked?

Does the class\_eval method allow the method\_missing method to be used?

> DummyClass.class\_eval “dummy\_method”  
> =\> “this dummy\_method was captured by #method\_missing”  
> DummyClass.class\_eval {dummy\_method}  
> =\> “this dummy\_method was captured by #method\_missing”

Yes. #method\_missing works as expected, in both string and block  
formats. So only #const\_missing is causing a problem.

Interestingly, an explicit call to #const\_missing inside a class\_eval  
block DOES work.

> DummyClass.class\_eval {const\_missing :ANOTHER\_CONSTANT}  
> =\> “this ANOTHER\_CONSTANT was captured by #const\_missing”

Anybody understand this, and is there a way to get around it?

Cheers

---

<div class="post-metadata">

**Author:** ![bfsog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bfsog](https://www.ruby-forum.com/u/bfsog)\
**Post date:** [April 21, 2011, 11:26pm UTC](https://www.ruby-forum.com/t/class-eval-doesnt-find-const-missing/206077/2 "2011-04-21T23:26:16Z")

</div>

If you add this code:

class Object  
def self.const\_missing(constant)  
return “this #{constant} found in obj!”  
end  
end

Then you get the following:

> > DummyClass.class\_eval “NAME”  
> > =\> “dummy constant”  
> > DummyClass.class\_eval {NAME}  
> > =\> “this NAME found in obj!”  
> > NAME  
> > =\> “this NAME found in obj!”

Not sure if that helps you much, but it seems that class\_eval doesn’t  
affect the constant lookup in the way that you’d like.

---

<div class="post-metadata">

**Author:** ![bfsog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bfsog](https://www.ruby-forum.com/u/bfsog)\
**Post date:** [April 21, 2011, 11:35pm UTC](https://www.ruby-forum.com/t/class-eval-doesnt-find-const-missing/206077/3 "2011-04-21T23:35:21Z")

</div>

> <https://stackoverflow.com/questions/3015947/ruby-how-does-constant-lookup-work-in-instance-eval-class-eval>

Const lookup varies between 1.8, 1.9.1, and 1.9.2.

Basically, constants are lexically scoped.

You can use “const\_get” I believe to lookup your constant in the  
current binding rather than in the lexical scope.

stephen

---

<div class="post-metadata">

**Author:** ![bfsog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bfsog](https://www.ruby-forum.com/u/bfsog)\
**Post date:** [April 22, 2011, 12:35am UTC](https://www.ruby-forum.com/t/class-eval-doesnt-find-const-missing/206077/4 "2011-04-22T00:35:47Z")

</div>

Andrew Berkeley wrote in post #994369:

> Interestingly, an explicit call to #const\_missing inside a class\_eval  
> block DOES work.
> 
> > DummyClass.class\_eval {const\_missing :ANOTHER\_CONSTANT}  
> > =\> “this ANOTHER\_CONSTANT was captured by #const\_missing”
> 
> Anybody understand this, and is there a way to get around it?

This line:

DummyClass.class\_eval {const\_missing :ANOTHER\_CONSTANT}

is equivalent to:

DummyClass.class\_eval {  
self.const\_missing(:ANOTHER\_CONSTANT)  
}

And because class\_eval() changes self inside the block to be equal to  
the receiver, the above is equivalent to:

DummyClass.class\_eval {  
DummyClass.const\_missing(:ANOTHER\_CONSTANT)  
}

…producing the output you see, which is to be expected.

Also, note the output here:

DummyClass.class\_eval do  
puts self::NAME  
end

–output:–  
dummy constant

In “The Well-Grounded Rubyist” David Black explains that constants are  
like files in a file system, and depending on what “directory” you are  
currently in, it will determine the “path name” to the constant that you  
are interested in retrieving. It seems that class\_eval does not affect  
the path name to a constant. But then I don’t understand how  
class\_eval’ing a string changes that.

> > DummyClass.class\_eval {NAME}  
> > =\> “this NAME found in obj!”  
> > NAME  
> > =\> “this NAME found in obj!”

> > Not sure if that helps you much, but it seems that class\_eval doesn’t  
> > affect the constant lookup in the way that you’d like.

That tells me that NAME is being looked up in the current scope, which  
is the block plus everything outside the block. Outside the block self  
is equal to main, and the current class is equal to Object. So when  
NAME isn’t found, Object.const\_missing is called.

---

<div class="post-metadata">

**Author:** ![bfsog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bfsog](https://www.ruby-forum.com/u/bfsog)\
**Post date:** [April 22, 2011, 1:14am UTC](https://www.ruby-forum.com/t/class-eval-doesnt-find-const-missing/206077/5 "2011-04-22T01:14:42Z")

</div>

Stephen Prater wrote in post #994391:

> 

[http://stackoverflow.com/questions/3015947/ruby-how-does-constant-lookup-work-in-instance-eval-class-eval](http://stackoverflow.com/questions/3015947/ruby-how-does-constant-lookup-work-in-instance-eval-class-eval)

> Const lookup varies between 1.8, 1.9.1, and 1.9.2.
> 
> Basically, constants are lexically scoped.
> 
> You can use “const\_get” I believe to lookup your constant in the  
> current binding rather than in the lexical scope.

That works for me in ruby 1.9.2:

DummyClass.class\_eval do  
puts const\_get(:NAME)  
end

–output:–  
dummy constant

That makes sense because the call is really:

puts self.const\_get(:NAME)

and because self is equal to DummyClass, that is equivalent to:

puts DummyClass.const\_get(:NAME)

which like class\_eval’ing a string serves to change the “directory” for  
the lookup to DummyClass.

---

<div class="post-metadata">

**Author:** ![bfsog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bfsog](https://www.ruby-forum.com/u/bfsog)\
**Post date:** [April 22, 2011, 1:29am UTC](https://www.ruby-forum.com/t/class-eval-doesnt-find-const-missing/206077/6 "2011-04-22T01:29:05Z")

</div>

7stud – wrote in post #994396:

> In “The Well-Grounded Rubyist” David Black explains that constants are  
> like files in a file system, and depending on what “directory” you are  
> currently in, it will determine the “path name” to the constant that you  
> are interested in retrieving. It seems that class\_eval does not affect  
> the path name to a constant.

> But then I don’t understand how  
> class\_eval’ing a string changes that.

Here is a blog post from 2007 asking the exact same question as the op:

[http://www.pgrs.net/2007/9/12/ruby-constants-have-weird-behavior-in-class\_eval](http://www.pgrs.net/2007/9/12/ruby-constants-have-weird-behavior-in-class_eval)

As one person replied, a constant is looked up by the parser, so the  
constant’s lexical scope determines the proper ‘path’ to the constant  
(which is the path from the ‘directory’ where the constant is referenced  
to the ‘directory’ in which the constant is defined)–while eval’ing a  
string happens in the ‘dynamic scope’ when the code is executed, i.e.  
inside the class.

---

<div class="post-metadata">

**Author:** ![bfsog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bfsog](https://www.ruby-forum.com/u/bfsog)\
**Post date:** [April 22, 2011, 9:43pm UTC](https://www.ruby-forum.com/t/class-eval-doesnt-find-const-missing/206077/7 "2011-04-22T21:43:23Z")

</div>

Thanks People - very helpful and seems pretty clear what’s going on  
then. A lesson learned for me!

The Object monkeypatch appears to work well for what I was wanting to  
do…
