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
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] File: MutableTime.rb
···
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.
Gavin,
This looks very interesting -- thanks for the pointer. I'll take a look
at it and see how i can adapt it to my situation.
(Sam, you were headed right along the same path so this certainly seems
to be the right way)
Thanks again,
Derek
Gavin Kistner wrote:
···
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] File: MutableTime.rb