[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

Gavin Kistner

2/10/2005 2:28:00 PM

On Feb 10, 2005, at 6:29 AM, Derek Wyatt wrote:
> 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.

In my MutableTime class [1], I wrapped Time, rather than inheriting
from it, and used method_missing to pass functions along to time.

Tiny snippets:

class MutableTime
include Comparable

def initialize( dateString_Seconds_Time_orYear = nil , *dateTimePieces
)
#...
@t=Time.now
end

def method_missing(meth, *args, &block) # :nodoc:
@t.send(meth, *args, &block)
end

def date=(n)
@t+=(n-date)*86400;
end

def +(n)
(d=self.dup).date+=n
d;
end
end


[1] http://phrogz.net/RubyLibs/rdoc/files/MutableTi...