I noticed if I make a subclass which inherits from Datetime, it’s .now will return the subclass instance, not Datetime instance.
class MyDateTime < DateTime
end
MyDateTime.now
>#<MyDateTime: 2012-06-05T16:42:57+08:00 ((2456084j,31377s,900801494n),+28800s,2299161j)>
It seems magical. I can’t reproduce this behavior in my own class:
class A
def self.a
return A.new
end
end
class B < A
end
B.a
#<A:0x00000001e22358>
I tried to read the source code of DateTime but it’s written in C. Is it possible to write a class method which returns an instance of the class it belongs to?
Try it with self: