# Module\_eval and scope question

**URL:** <https://www.ruby-forum.com/t/module-eval-and-scope-question/125880>\
**Category:** Rails\
**Created:** [January 13, 2008, 9:24am UTC](https://www.ruby-forum.com/t/module-eval-and-scope-question/125880 "2008-01-13T09:24:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ronny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/94ad74/32.png) [@ronny](https://www.ruby-forum.com/u/ronny)\
**Post date:** [January 13, 2008, 9:24am UTC](https://www.ruby-forum.com/t/module-eval-and-scope-question/125880/1 "2008-01-13T09:24:56Z")

</div>

When trying to understand a piece of code from the Ruby-On-Rails  
framework,  
I found that I’m still lacking knowledge about scope issues in Ruby.  
Here is a  
stripped down version of the code I’m trying to understand (in case  
you  
happen to use Rails: This is from file scaffolding.rb):

module ActionController  
module Scaffolding  
…  
module ClassMethods  
def scaffold(model\_id, options = {})  
…  
unless options[:suffix]  
module\_eval \<\<-“end\_eval”, **FILE** , **LINE**  
def index  
list  
end  
end\_eval  
end  
…  
end  
end  
end

The purpose of this code is to create at run-time the function  
‘index’.  
My question is: Why do I need ‘module\_eval’ here? I understand that  
I can use module\_eval to evaluate an expression in the context of  
a different class/module, for example

mymodule.module\_eval \<\<“END”  
def foo  
end  
END

would define foo in the context of mymodule. In the example code  
above,  
however, there is no SOMETHING.module\_eval defined, so the scope  
would be ClassMethods anyway and, so I had thought, the author could  
have written simply:

module ActionController  
module Scaffolding  
…  
module ClassMethods  
def scaffold(model\_id, options = {})  
…  
unless options[:suffix]  
def index  
list  
end  
end  
…  
end  
end  
end

Of course I’m pretty sure that there was some purpose in using  
module\_eval  
here, though. Could anybody enlighten me why it is necessary, and why  
my  
simpler solution would not work?

I posted this question already in Ruby-Talk, and got the comment

“I believe the answer is that it’s evaluated at run-time, so the  
module\_eval refers to a different module, but I’m not sure about that.  
It could just be that the Rails guys screwed up or something.”

which is not entirely convincingly to me. Maybe some Rails expert  
could either  
confirm that comment or propose a different answer?

Ronald

---

<div class="post-metadata">

**Author:** ![ronny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/94ad74/32.png) [@ronny](https://www.ruby-forum.com/u/ronny)\
**Post date:** [January 13, 2008, 5:29pm UTC](https://www.ruby-forum.com/t/module-eval-and-scope-question/125880/2 "2008-01-13T17:29:18Z")

</div>

On Jan 13, 2008 7:24 PM, Ronny [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Of course I’m pretty sure that there was some purpose in using  
> module\_eval  
> here, though. Could anybody enlighten me why it is necessary, and why  
> my  
> simpler solution would not work?

With the #module\_eval, #index becomes an instance method of self  
(which would be the client code’s controller class at this point).

Without the #module\_eval, #index becomes an instance method of  
ClassMethods. Since this module is not #included in anything (it  
#extends a class, but is not #include-d in one), this does nothing  
useful.

Here’s a concentrated version of what’s going on. Give it a run and  
see if it clears things up.

module ClassMethods  
def add\_f  
module\_eval \<\<-EOS  
def f  
p self  
end  
EOS  
end  
end

class C  
extend ClassMethods  
add\_f  
end

p C.instance\_methods.include?(‘f’)  
p ClassMethods.instance\_methods.include?(‘f’)

Regards,  
George.

---

<div class="post-metadata">

**Author:** ![ronny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/94ad74/32.png) [@ronny](https://www.ruby-forum.com/u/ronny)\
**Post date:** [January 14, 2008, 12:12pm UTC](https://www.ruby-forum.com/t/module-eval-and-scope-question/125880/3 "2008-01-14T12:12:40Z")

</div>

> With the #module\_eval, #index becomes an instance method of self  
> (which would be the client code’s controller class at this point).
> 
> Without the #module\_eval, #index becomes an instance method of  
> ClassMethods. Since this module is not #included in anything (it  
> #extends a class, but is not #include-d in one), this does nothing  
> useful.

Thank you so much! This really clarifies it!!

Ronald
