[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Subclassing Immutable objects (Time

Sam Roberts

2/10/2005 2:21:00 PM

How about not sub-classing, and have Time be an implmentation detail
of your class? You can use method_missing to forward all methods
you don't specifically implement to your @time attribute, and you
can be mutable by just replacing your @time.

Sam

Quoteing tone_hole@yahoo.ca, on Thu, Feb 10, 2005 at 10:29:45PM +0900:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hi folks,
>
> I've been trying for a while to subclass Time, but i'm hitting some
> problems. I tried simply overriding the methods in Time itself but
> after about 5 minutes i realized that i needed the old behaviour as
> well, so subclassing seems better. I'm hitting the same problems with
> wrapping the object as well.
>
> I know this question has been asked before but as far as i can tell they
> all seemed to deal with mutable objects (one notable example dealt with
> Array which has a nice replace() method available to it)
>
> Because Time is immutable, there are a ton of methods that return new
> Time objects (class method parse, instance methods +, and - are the
> notable ones since i'm interested in these). So if i write:
>
> class MyTime < Time
> end
>
> And do the following:
>
> time = MyTime.parse("00:00:00") # returns Time
> time = MyTime.now + 50.0 # returns Time
>
> Now, i could reimplement these methods in MyTime to return MyTime
> objects but I'm lost on how to do the conversion without something like
> Array's replace() method. If i had it then i could do:
>
> class MyTime < Time
>
> ~ def MyTime.parse(str)
> ~ now.replace(Time::parse(str))
> ~ end
>
> ~ def +(other)
> ~ replace(Time::+(other))
> ~ end
>
> end
>
> or something along those lines.
>
> Anyone have any tips?
>
> Thanks,
> Derek
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.2.4 (MingW32)
>
> iD8DBQFCC2G5xPPkePIbSlwRAurHAJ90WXo/+bMwOD/AxQD7Nz3sUqvx9ACglIO5
> EcR7mKvC2Zb4x3amefaWnl4=
> =sYWa
> -----END PGP SIGNATURE-----
>