From: Stefan Rusterholz Date: 2007-05-28T20:36:39+09:00 Subject: Re: Tricky problem with === unknown wrote: > Hi -- > > On Mon, 28 May 2007, Erwin Abbott wrote: > >> time. It's job is to return true if the given time is more than a week >> and look up the documentation. I agree distinguishing objects by class >> #old_name and making sure my #name doesn't break any code in that >> class. I might still break something with a proxy class answering >> #name, but it won't break the underlying object when it calls name on >> itself. I think at this point I'll accept that case statements won't >> work, but I'm going to try a few other things before giving up. > > I was thinking of something like (semi-tested only): > > module DateComp > def <(other) > Time.parse(strftime("%m/%d/%y")) < other > end > end > > d = Date.new.extend(DateComp) > your_method(d) > > That way, you can use duck typing in the method itself, which means > that you don't have to change the method if you want to provide it > with other objects; you just have to prepare things on the calling > end. You might have to have a more complex mixin for your proxy case, > but anyway, that's the kind of thing I was thinking about. > > > David Universal Date/Time comparison is indeed difficult as neither your nor mine (below) solution attribute for different TimeZones and/or DST. I'd implement a universal comparison like below: def <=>(other) [year, day_of_year, hour, minute, second] <=> [other.year, other.day_of_year, other.hour, other.minute, other.second] end Regards Stefan -- Posted via http://www.ruby-forum.com/.