[LRUG] Finding the duration between two DateTimes
Eleanor
eleanor at goth-chic.org
Mon May 28 06:58:39 PDT 2007
On 27 May 2007, at 14:17, Timothy Cowlishaw wrote:
> However, I'd still be very curious to know if this can be casted as
> some sort of class that represents a duration, rather than simply as
> an integer. Any ideas?
The simple answer is yes, assuming you want to do things with the
duration above and beyond storing it as a time. For example, if you
wanted to keep track of a duration whilst imposing a modifiable limit
on it you might use the following Ruby code:
class LimitExceeded < Exception
end
class Duration
attr_writer :length, :limit
def initialize length = 0, limit = 0
@length = length
@limit = limit
end
def exceeds? value
@length > value
end
def exceeds_limit?
exceeds? @limit
end
def + time
@length += time
raise LimitExceeded if exceeds_limit?
end
def extend_limit time
@limit += time
end
end
Then where you currently create your duration you could write:
duration = Duration.new(Task.duration)
However from the perspective of a Task object you may not need any of
this complexity. Just because something can be made into an object
doesn't necessarily mean that it should...
Ellie
Eleanor McHugh
Games With Brains
----
raise ArgumentError unless @reality.responds_to? :reason
More information about the Chat
mailing list