I can define an anonymous class within an anonymous module:
c = nil
m = Module.new do
c = Class.new
end
m #=> #<Module:0x007fad3a055660>
c #=> #<Class:0x007fad3a0555e8>
Is the above equivalent to:
m = Module.new
c = Class.new
In other words: does the concept of "nesting" actually apply to anonymous modules?
It is not about being anonymous. Assigning a dynamically created class to a constant makes it named:
Yet it still doesn't nest further:
Or even about being dynamic for that matter:
It's about scope gates.
As far as constants are concerned, there are only two scope gates - the keywords
class
andmodule
.Nesting is done purely syntactically. That is why you get the weird:
So if Ruby sees the keywords
class
ormodule
, it nests the "current node" further. When the closingend
is found, it goes up the tree. When a new constant is being defined, it places it in the current node.