A.module_eval question

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?

On 11.01.2010 21:42, Aryk G. wrote:

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

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