In self.inherited method of base class, subclass is passed, calling subclass's name method instead calls base class method. Though same thing works If same method is called on same class elsewhere
class A
def self.name
"a"
end
def self.inherited(subclass)
puts B.hash
puts B.name
end
end
class B < A
def self.name
"b"
end
end
puts B.hash
puts B.name
output:
1428955046062147697
a
1428955046062147697
b
No magic here.
When you declare
Bthe things happen in the following order (roughly speaking):B(an instance ofClass) is created (which inherits everything fromA). At this moment it doesn't have anything specific.A.inheritedhook is invoked.Bclass is opened and processed. Only at this point, it gets its own properties and methods (except the ones that could be created inside the hook).So, when (2) happens the only
namethat is available forBis the one defined inA.This is very easy to check using the following code:
Everything is clear now, right?