From: Hermann Martinelli Date: 2007-05-15T06:35:08+09:00 Subject: Re: Ruby Inheritance Question Mike Hamilton wrote: > I'm very confused at the moment regarding inheritance. I have a class > that inherits from Time as in: > > class RTDate < Time > ...my methods adding to the class > end > > I have the initialize method inherit from the parent class. Here is > where I get confused: > irb(main):001:0> require 'rtdate2.rb' > => true > irb(main):002:0> r = RTDate.new > => Mon May 14 09:12:06 PDT 2007 > irb(main):003:0> r.class > => RTDate > irb(main):004:0> s = r.yesterday > => Sun May 13 09:12:06 PDT 2007 > irb(main):005:0> s.class > => Time > irb(main):006:0> > > Here is what I don't understand - why does s become a Time object At first I did not understand why this should work at all. You do not show where you have defined Time#yesterday. Assuming you did use the -() method: You inherit the -() method (the minus operator), which by definition returns a Time object. You can overload the minus operator or explicitly define the yesterday method. Simple but easy to understand example: # Code starts class RTDate < Time def yesterday # Create a new (!) RTDate (!) object: RTDate.at(self - 86400) end end r = RTDate.new puts r.yesterday, r.yesterday.class # Code ends Output: Mon May 14 23:26:32 +0200 2007 Sun May 13 23:26:32 +0200 2007 RTDate