Ruby Inheritance Question

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 - (606024)
end
end

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.

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

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!

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.

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 - (606024)
end
end

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

class RTTime < Time ; def yesterday ; self - 606024 ; 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

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