module M
end
M.module_eval { def self.a; puts ‘ok’ end }
M.a
=> ‘ok’
M.module_eval { class A; end }
M::A
module M
end
M.module_eval { def self.a; puts ‘ok’ end }
M.a
M.module_eval { class A; end }
M::A
2008/8/18 Kyung won Cheon [email protected]:
=> uninitialized constant M::A (NameError)
Constants (including class and module names) are lexically scoped (at
least in Ruby 1.8). Your code therefore defines the top-level class A.
If you want a nested constant, you could do
M.module_eval { class self::A; end }
Regards,
Pit
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs