Module_eval and scope question

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?

Ronald

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.


Giles B.

Podcast: http://hollywoodgrit.blogspot.com
Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs