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?

···

--
Posted via http://www.ruby-forum.com/.

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

···

On 11.01.2010 21:42, Aryk Grosz wrote:

I'm having trouble understanding why this is:

module A
module B
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?

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/