Hi --

On Fri, 26 Aug 2005, Eric Mahurin wrote:

> --- "David A. Black" <dblack / wobblini.net> wrote:
>
>>> The klass.from_s(str) form also offers a little more power.
>>  If
>>> you are dealing with an arbitrary class (possibly of
>> another
>>> object) and you want to convert a String to an object of
>> that
>>> class, you just call the from_s method of that class.  You
>>> don't have to go figure out what the right String#to_*
>> method
>>> to call is based on the class (and violate duck typing).
>>
>> Duck typing is irrelevant here.
>
> I think an example is needed to demonstrate my point.  Say you
> have a command line parser where you want to convert options
> from String to the relavent class.  Somewhere in the
> specification, you may specify the class that you want the
> option to be.  With the String#to_* methods, you have to do
> something like this:
>
> def parse_option(klass,option)
>  if klass.equal?(String)
>    option
>  elsif klass.equal?(Integer)
>    option.to_i
>  elsif klass.equal?(Float)
>    option.to_f
>  else
>    raise("can't convert #{option} to #{klass}")
>  end
> end
>
> With klass.from_s(str) methods, here is what you get:
>
> def parse_option(klass,option)
>  klass.from_s(option)
> end
>
> Which is duck-typed?

Neither of them would I include in, say, an essay, on duck typing, not
because they "violate" it (it's not a rule) but because it's not
relevant.  Once you're passing class names around and doing explicit
conversions based on class, that's what you're doing, and you might as
well embrace it.

In the case of your example, I would probably do it like this:

   def parse_option(option)
     #...
   end

   parse_option(3)
   parse_option(str.to_f)
   parse_option(MyClass.from_s(str))  # if you must :-)

etc. -- and then you can duck-type the incoming argument all you like
without having to convert it.  It's very cumbersome to have a method
whose arguments have to be: an object, and a class which responds to
from_s and whose from_s method takes objects of the class of *this*
object as arguments....  Better to do it before you call the method.


David

-- 
David A. Black
dblack / wobblini.net