From: mark sparshatt Date: 2003-09-12T04:12:31+09:00 Subject: Re: ANN: MetaTags 1.0 Richard Kilmer wrote: > > On Thursday, September 11, 2003, at 01:31 AM, Martin DeMello wrote: > >> Dave Thomas wrote: >> >>> >>> Or perhaps even using the '>>' operator (as in 'becomes' or 'inject >>> into') >>> >>> c = "one hump or two" >> Caml >> >> >> Nice! And perhaps the other way around too - I notice that Class#<< is >> undefined. >> >> c = Caml << "one hump or two" (read 'from') >> >> martin >> > > Oh my...improving on perfection! You could make this type-selectable: > > class Date > def self.__String(string) > puts "String factory" > Date.new > end > def self.__Fixnum(millis) > puts "Fixnum factory" > Date.new > end > end > > class Class > def <<(object) > sym = "__#{object.class}".intern > return send(sym, object) if respond_to?(sym) > return nil > end > end > > d = Date << "String" > d = Date << 1234543 > > The generic factory pattern! > > I like the general idea. But I'd prefer it if the << operator raised an error if the type isn't supported instead of returning null Something like class Class def <<(object) sym = "__#{object.class}".intern return send(sym, object) if respond_to?(sym) raise TypeError end end Best regards Mark Sparshatt