inheritance - ruby - return an instance of current class -


i noticed if make subclass inherits datetime, it's .now return 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. can't reproduce behavior in own class:

class   def self.a    return a.new   end end  class b < end  b.a #<a:0x00000001e22358> 

i tried read source code of datetime it's written in c. possible write class method returns instance of class belongs to?

try self:

class   def self.a    return self.new   end end  class b < end  b.a 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -