From: Benoit Daloze Date: 2011-08-07T03:24:26+09:00 Subject: [ruby-core:38826] Re: [Ruby 1.9 - Bug #5148][Open] Constant Namespace/Scoping in Dynamic Classes/Modules Hi, On 2 August 2011 17:01, Thomas Sawyer wrote: > Bug #5148: Constant Namespace/Scoping in Dynamic Classes/Modules > http://redmine.ruby-lang.org/issues/5148 > > When defining a dynamic class or module, the constants defined within it are not kept within it's namespace, but are tied to the outer scope's namespace. > > � �c = Class.new do > � � �module M; end > � �end > � �#=> # > � �c::M �#=> M > � �M � � #=> M I also noticed that recently. It seems inconsistent to me. Let's have some code: https://gist.github.com/1129567 Constant scoping seems to depend on the the fact it is defined in a "dynamic" scope or not. But one can force to define on a specific scope by doing scope::Const = value (like self::C = value in a Class.new block). I think the constant definition scope should always be the current scope (as you would always add "self::" before the constant name). Also, this should be true for constant resolution, I should not need self.class::CONST, in a parent class to resolve a child class's constant. To "solve" your problem, you could use self:: describe "unfancy mixin" do class self::Foo # not at toplevel include UnfancyMixin end # Foo # uninitialized constant Foo (NameError) self::Foo # => #::Foo const_get :Foo # => #::Foo end But it has an immediate pitfall: you can not reference it by 'Foo', you need 'self::Foo' or 'const_get :Foo'. Variables and method calls are fully dynamic for definition/resolution, in opposition to constants. Can someone explain the rationale behind this logic ?