From: Gavin Kistner Date: 2005-02-10T23:28:17+09:00 Subject: Re: Subclassing Immutable objects (Time) 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/MutableTime_rb.html