# Idiom or method for determining where in the class hierarchy an object fits

**URL:** <https://www.ruby-forum.com/t/idiom-or-method-for-determining-where-in-the-class-hierarchy-an-object-fits/193934>\
**Category:** Ruby\
**Created:** [August 26, 2010, 8:15pm UTC](https://www.ruby-forum.com/t/idiom-or-method-for-determining-where-in-the-class-hierarchy-an-object-fits/193934 "2010-08-26T20:15:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Collins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@Collins](https://www.ruby-forum.com/u/Collins)\
**Post date:** [August 26, 2010, 8:15pm UTC](https://www.ruby-forum.com/t/idiom-or-method-for-determining-where-in-the-class-hierarchy-an-object-fits/193934/1 "2010-08-26T20:15:42Z")

</div>

I have a class hierachy that looks like this

class A  
…  
end

class B \< A  
…  
end

class C \< A  
…  
end

class D \< B  
…  
end

class E \< C  
…  
end

class F \< B  
…  
end

Now class B has a method that expects either members of class D or E  
or Fas a parameter but needs to do something different based upon the  
whether the parameter is a subclass of B or a subclass of C.  
I could check to see if it was D or F as opposed to B but that would  
mean when I added class G\<B…end I’d have to go back and touch B.  
Is there a way to ask an object if it is descended from a particular  
class? I suppose I could add a method to B and test for the presence  
of that method in the parameter objects but that feels ucky.

Thanks

```
Collins
```

---

<div class="post-metadata">

**Author:** ![Collins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@Collins](https://www.ruby-forum.com/u/Collins)\
**Post date:** [August 26, 2010, 8:30pm UTC](https://www.ruby-forum.com/t/idiom-or-method-for-determining-where-in-the-class-hierarchy-an-object-fits/193934/2 "2010-08-26T20:30:55Z")

</div>

On Aug 26, 2010, at 2:15 PM, Collins wrote:

> class C \< A
> 
> class? I suppose I could add a method to B and test for the presence  
> of that method in the parameter objects but that feels ucky.
> 
> Thanks
> 
> Collins

You could try kind\_of? e.g.

> > o = F.new  
> > =\> #\<F:0x00000100a74a30\>  
> > o.kind\_of?(A)  
> > =\> true  
> > o.kind\_of?(B)  
> > =\> true  
> > o.kind\_of?(C)  
> > =\> false

etc.

Hope this helps,

Mike

–

Mike S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.stok.ca/~mike/](http://www.stok.ca/~mike/)

The “`Stok’ disclaimers” apply.

---

<div class="post-metadata">

**Author:** ![Collins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@Collins](https://www.ruby-forum.com/u/Collins)\
**Post date:** [August 26, 2010, 8:40pm UTC](https://www.ruby-forum.com/t/idiom-or-method-for-determining-where-in-the-class-hierarchy-an-object-fits/193934/3 "2010-08-26T20:40:53Z")

</div>

On Aug 26, 2:30 pm, Mike S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > end  
> > …  
> > Now class B has a method that expects either members of class D or E  
> > Collins  
> > =\> false
> 
> The “`Stok’ disclaimers” apply.

Thank you! I was only aware of instance\_of? This is exactly what I  
was hoping to find…

Thanks again

```
Collins

```
