Yep. That's the same priority I have - no checking
(preferred), respond_to?, and finally kind_of? (least
preferred). The only time I'm using kind_of? now is
"kind_of?Numeric". Even where I want integers, I found Numeric
more useful to handle cases where +/- infinity and even -0
(-0.1) makes sense. For example, a -0 would have been useful
as an index for String/Array#.
Discover Yahoo!
Stay in touch with email, IM, photo sharing and more. Check it out!
http://discover.yahoo.com/stayintouch.html
ยทยทยท
--- "David A. Black" <dblack@wobblini.net> wrote:
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