Ruby Inheritance Question

M

Mike Hamilton

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
 
S

Sean T Allen

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.
 
M

Mike Hamilton

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!
 
M

Mike Hamilton

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!
 
B

Bertram Scharpf

Am Dienstag, 15. Mai 2007, 01:11:51 +0900 schrieb Mike Hamilton:
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>

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,233
Latest member
AlyssaCrai

Latest Threads

Top