A.module_eval question

A

Aryk Grosz

I'm having trouble understanding why this is:

module A
module B
end
end

A.module_eval "B" -> A::B
A.module_eval do B end -> ERROR!
A.module_eval do self::B end => A::B

Why does the second one error out? If its actually being evaluated
within the scope of the module, it should work...no?


Could someone explain this to me. Is there a trick I'm not doing?
 
R

Robert Klemme

I'm having trouble understanding why this is:

module A
module B
end
end

A.module_eval "B" -> A::B
A.module_eval do B end -> ERROR!
A.module_eval do self::B end => A::B

Why does the second one error out? If its actually being evaluated
within the scope of the module, it should work...no?


Could someone explain this to me. Is there a trick I'm not doing?

What version of Ruby did you use? IIRC in 1.8 versions the lookup of
the constant was statically scoped so - since there is no B around - you
get an error. With the other approaches you explicitly start the lookup
at module A. We had a recent discussion about this where I collected
some links about the topic:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/354034

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,427
Latest member
HildredDic

Latest Threads

Top