Hi --

On Wed, 11 May 2005, Eric Mahurin wrote:

>>> Thanks.  I assumed that the StringIO class inherited from
>> the
>>> IO class since it has all of the same methods.  So the
>> proper
>>> Ruby style is to use respond_to? instead of kind_of? to see
>>> what "type" your arguments are?
>>
>> The common Ruby style is to not check at all, but just send
>> the messages
>> needed to get the job done.  If the object is inappropriate,
>> Ruby will
>> catch it and let you know what it didn't like.
>
> I was referring to the case when a method does different things
> based on the argument "types" (i.e. String#[]=).  I'll just
> start using "respond_to?" instead.  I was using both.

I think Jim was referring to that case too :-)

In a slightly oversimplified nutshell, you've got three main
possibilities:

   * class/module name/ancestry checking (kind_of?/is_a?)
   * type checking (respond_to?)
   * "duck typing" (what Jim described -- a programming technique
       in its own right, not a subdivision of checking)

I've listed them in increasing order of what I'd call resonance with
the underlying dynamism of Ruby objects.  In a certain sense, in fact,
you're always doing duck typing when you send a message to an object
in Ruby, because you've agreed up front to work with objects whose
type can change during runtime.

"And etc.", as my students sometimes say.  There's a ton more on this
in the archives... so I'll save a few electrons here :-)


David

-- 
David A. Black
dblack / wobblini.net