When the interpreter is going through this file, it is assigning classes like this:
OuterClass = Class.new do
def foobar
puts "FOOBAR"
end
InnerClass = Class.new do
def barfoo
puts "BARFOO"
end
end
end
So when Ruby encounters the nested class, it assigns it to constant InnerClass
which is assigned to constant OuterClass
They have no relation to each other whatsoever.
The InnerClass
has no inheritance to OuterClass
:
InnerClass.ancestors
=> [InnerClass, Object, Kernel, BasicObject]
When you call OuterClass::InnerClass
constant you are refering to the InnerClass
constant that is namespaced under the OuterClass
contstant which equals the Class.new statement assigned to it.
A good book to read about this is “Metaprogramming Ruby”. It goes into the details of classes, singletons, modules etc.