[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: ANN: MetaTags 1.0

Richard Kilmer

9/11/2003 6:56:00 PM


On Thursday, September 11, 2003, at 01:31 AM, Martin DeMello wrote:

> Dave Thomas <Dave@pragprog.com> 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!


1 Answer

mark sparshatt

9/11/2003 7:13:00 PM

0

Richard Kilmer wrote:

>
> On Thursday, September 11, 2003, at 01:31 AM, Martin DeMello wrote:
>
>> Dave Thomas <Dave@pragprog.com> 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