# A class that looks like (kind\_of?) several classes

**URL:** <https://rubytalk.org/t/a-class-that-looks-like-kind-of-several-classes/18491>\
**Category:** ruby-talk\
**Created:** [18 May 2005 17:20 UTC](https://rubytalk.org/t/a-class-that-looks-like-kind-of-several-classes/18491 "2005-05-18T17:20:43Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_Mahurin1](https://avatars.discourse-cdn.com/v4/letter/e/34f0e0/32.png) [@Eric\_Mahurin1](https://rubytalk.org/u/Eric_Mahurin1)\
**Post date:** [18 May 2005 17:20 UTC](https://rubytalk.org/t/a-class-that-looks-like-kind-of-several-classes/18491/1 "2005-05-18T17:20:43Z")

</div>

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](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:
> > 
> > &nbsp;&nbsp;&nbsp;\* class/module name/ancestry checking (kind\_of?/is\_a?)  
> > &nbsp;&nbsp;&nbsp;\* type checking (respond\_to?)  
> > &nbsp;&nbsp;&nbsp;\* "duck typing" (what Jim described -- a programming  
> > technique  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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 🙂
