From: "fxn (Xavier Noria)" Date: 2012-07-29T21:38:09+09:00 Subject: [ruby-core:46855] [ruby-trunk - Bug #6810] `module A::B; end` is not equivalent to `module A; module B; end; end` with respect to constant lookup (scope) Issue #6810 has been updated by fxn (Xavier Noria). They are very decoupled, name is the only bit mostly in common, set on constant assignment. May I link to a talk of mine, which covers this (content constrained by the 30min slot): http://www.youtube.com/watch?v=wCyTRdtKm98. In particular, the name of the modules is unrelated to the constants they are stored in. See for example module M end N = M module A B = M end Now, the module object that is *stored* in the constant M, it is also stored in the constant N, and in the constant A::B. It can be stored in a hundred places. And the constant M can disappear: Object.instance_eval { remove_const(:M) } So that module object whose name is "M" is well and alive. You can reopen, mixin the module... everything. The constant M is no longer available, but that is irrelevant as far as Ruby is concerned. Modules are objects, constants just storage, formally they are very superficially related to each other, although in practice we often identify them of course. ---------------------------------------- Bug #6810: `module A::B; end` is not equivalent to `module A; module B; end; end` with respect to constant lookup (scope) https://bugs.ruby-lang.org/issues/6810#change-28530 Author: alexeymuranov (Alexey Muranov) Status: Open Priority: Normal Assignee: Category: core Target version: ruby -v: ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin10.8.0] =begin Is this the expected behavior? To me it is rather surprising: N = 0 module A module B def self.f; N; end end N = 1 end A::B.f # => 1 but N = 0 module A; end module A::B def self.f; N; end end module A N = 1 end A::B.f # => 0 Even more striking: module A module B def self.f; N; end end end N = 0 A::B.f # => 0 A::N = 1 puts A::B.f # => 1 A::B::N = 2 A::B.f # => 2 but module A; end module A::B def self.f; N; end end N = 0 A::B.f # => 0 A::N = 1 A::B.f # => 0 A::B::N = 2 A::B.f # => 2 =end -- http://bugs.ruby-lang.org/