From: mathew
Date: 2005-09-30T14:01:45+09:00
Subject: Re: Overloading
Edward Faulkner wrote:
> On Mon, Sep 26, 2005 at 10:38:16AM +0900, Ben wrote:
>>The options I see are:
>>
>>1) Call .class.to_s on each parameter to match the type:
>> "pattern.class.to_s =~ /Array/ then # I have an array of regexp
>
>
> You can do this:
>
> if pattern.class == Array # then ...
Better is:
if pattern.kind_of?(Array)
...
end
because someone might want to subclass arrays and still be able to pass
them to your code. If you demand only unmodified array classes, you're
making the code unnecessarily inflexible.
Even better would be to test for the specific methods you need using
object.responds_to?(selector), i.e. to use duck typing. For example,
you might need an enumerable object, in which case you'd check if
pattern.responds_to?(each). Then I could pass in a SQL query response
that happened to be enumerable and return patterns when each element was
coerced via #to_s...
mathew
--
WE HAVE TACOS