# Ruby Inheritance Question

**URL:** <https://www.ruby-forum.com/t/ruby-inheritance-question/104140>\
**Category:** Ruby\
**Created:** [May 14, 2007, 6:11pm UTC](https://www.ruby-forum.com/t/ruby-inheritance-question/104140 "2007-05-14T18:11:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [May 14, 2007, 6:11pm UTC](https://www.ruby-forum.com/t/ruby-inheritance-question/104140/1 "2007-05-14T18:11:50Z")

</div>

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  
instead of an RTDate object. If I do s = r, then I can get all of my  
methods out because s becomes an RTDate object. I’m really confused  
because the yesterday method is specific to my RTDate class so how can  
the resulting object be a time?  
Below is an excerpt from my class thus far:  
class RTDate \< Time  
def yesterday  
self - (60_60_24)  
end  
end

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [May 14, 2007, 6:19pm UTC](https://www.ruby-forum.com/t/ruby-inheritance-question/104140/2 "2007-05-14T18:19:27Z")

</div>

Because you are returning the result of ‘-’.

If you don’t specifically have that return an RTDate, it will return the  
result of Time ‘-’  
which is either:

time - time =\> float  
time - numeric =\> time

yours is the second.

As ruby classes are ‘open’ and you can add methods to them, inheritance  
might not even  
be how you want to approach this problem. If RTDate is adding methods  
and not storing  
additional instance or class data, you should just be adding yesterday  
and whatever else  
as part of the Time class.

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [May 14, 2007, 6:20pm UTC](https://www.ruby-forum.com/t/ruby-inheritance-question/104140/3 "2007-05-14T18:20:27Z")

</div>

The ‘-’ (minus) method is defined in Time, and it returns a Time  
instance.

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [May 14, 2007, 6:27pm UTC](https://www.ruby-forum.com/t/ruby-inheritance-question/104140/4 "2007-05-14T18:27:55Z")

</div>

So can I get around this by creating a ‘-’ method in my own class or is  
there a smarter way? I’m really new to this so input is very valuable!

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [May 14, 2007, 6:55pm UTC](https://www.ruby-forum.com/t/ruby-inheritance-question/104140/5 "2007-05-14T18:55:55Z")

</div>

OK - I figured out what you mean! I created a library with a class  
called Time and added my methods and it all worked great!!! Thanks for  
the tip, I had no idea you could do that in ruby!

Sean T Allen wrote:

> Because you are returning the result of ‘-’.
> 
> If you don’t specifically have that return an RTDate, it will return the  
> result of Time ‘-’  
> which is either:
> 
> time - time =\> float  
> time - numeric =\> time
> 
> yours is the second.
> 
> As ruby classes are ‘open’ and you can add methods to them, inheritance  
> might not even  
> be how you want to approach this problem. If RTDate is adding methods  
> and not storing  
> additional instance or class data, you should just be adding yesterday  
> and whatever else  
> as part of the Time class.

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [May 14, 2007, 7:56pm UTC](https://www.ruby-forum.com/t/ruby-inheritance-question/104140/6 "2007-05-14T19:56:42Z")

</div>

Am Dienstag, 15. Mai 2007, 01:11:51 +0900 schrieb Mike H.:

> =\> true  
> Below is an excerpt from my class thus far:  
> class RTDate \< Time  
> def yesterday  
> self - (60_60_24)  
> end  
> end

The Operator - return a Time. Cast it to RTDate resp. RTTime.

class RTTime \< Time ; def yesterday ; self - 60_60_24 ; end ; end  
r = RTTime.now

r.yesterday # =\> Sun May 13 19:49:57 +0200 2007  
r.yesterday.class # =\> Time

class RTTime ; def - oth ; RTTime.at super ; end ; end

r.yesterday # =\> Sun May 13 19:49:57 +0200 2007  
r.yesterday.class # =\> RTTime

Bertram

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [May 14, 2007, 11:36pm UTC](https://www.ruby-forum.com/t/ruby-inheritance-question/104140/7 "2007-05-14T23:36:20Z")

</div>

Mike H. wrote:

> =\> true  
> 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
