Your question is actually not about method overriding. It is about what class is referred to within a class ...
construction in a module body.
When you do
module Bar
class << Foo
p self
end
end
# => #<Class:Foo>
the << Foo
points to the singleton class of the Foo
in the main environment because class << Foo
cannot define the singleton class directly of a class Foo
that has not been defined in advance. So it looks up for Foo
that is already defined, and such class is found in the main environment.
When you do
module Bar
class Foo
p self
end
end
# => Bar::Foo
a new class Bar::Foo
is created; the Foo
points to this Bar::Foo
that is newly created, and it does not point to the Foo
in the main environment. In order to point to it, you have to explicitly specify that with ::
.
module Bar
class ::Foo
p self
end
end
# => Foo