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
on 2007-05-14 20:11
on 2007-05-14 20:19
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.
on 2007-05-14 20:20
The '-' (minus) method is defined in Time, and it returns a Time instance.
on 2007-05-14 20:27
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!
on 2007-05-14 20:55
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.
on 2007-05-14 21:56
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
on 2007-05-15 01:36
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