# Finding parent-class hierarchy recursively

**URL:** <https://www.ruby-forum.com/t/finding-parent-class-hierarchy-recursively/148470>\
**Category:** Ruby\
**Created:** [September 13, 2008, 5:20am UTC](https://www.ruby-forum.com/t/finding-parent-class-hierarchy-recursively/148470 "2008-09-13T05:20:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [September 13, 2008, 5:20am UTC](https://www.ruby-forum.com/t/finding-parent-class-hierarchy-recursively/148470/1 "2008-09-13T05:20:43Z")

</div>

Hi,

I wrote a method to display a class’ “parentage” based on an idea in  
“Ruby Cookbook”, sec. 10.1:

def parents(obj)  
( (obj.superclass ? parents(obj.superclass) : []) \<\< obj). reverse  
end

I displayed Class’ parent-class hierarchy with:

Class.superclass.inspect =\> Module  
Module.superclass.inspect =\> Object  
Object.superclass.inspect =\> nil

Then I tested the automatic generation of that hierarchy as follows:

parents(Class).inspect =\> [Class, Object, Module]  
Expected: [Class, Module, Object]

I tried display intermediate values during the recursion, but I  
couldn’t see where I’m going wrong. Any ideas?

Thanks in advance,  
Richard

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [September 13, 2008, 11:29am UTC](https://www.ruby-forum.com/t/finding-parent-class-hierarchy-recursively/148470/2 "2008-09-13T11:29:18Z")

</div>

RichardOnRails wrote:

> def parents(obj)  
> ( (obj.superclass ? parents(obj.superclass) : []) \<\< obj). reverse  
> end  
> […]  
> parents(Class).inspect =\> [Class, Object, Module]  
> Expected: [Class, Module, Object]

Ok, here’s what happens:  
parents(Class) = (parents(Module) \<\< Class).reverse  
parents(Module) = (parents(Object) \<\< Module).reverse  
parents(Object) = [Object]  
parents(Module) = ([Object] \<\< Module).reverse  
= [Module, Object]  
parents(Class) = ([Module, Object] \<\< Class).reverse  
= [Module, Object, Class].reverse  
= [Class, Object, Module]

Anyway, do you know that you can get what you want just by calling  
TheClass.ancestors? Well, not quite as that also includes included  
modules, but TheClass.ancestors.grep(Class) would give you exactly  
the results you expected from your method.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [September 14, 2008, 3:20am UTC](https://www.ruby-forum.com/t/finding-parent-class-hierarchy-recursively/148470/3 "2008-09-14T03:20:52Z")

</div>

On Sep 13, 5:21 am, Sebastian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> parents(Module) = (parents(Object) \<\< Module).reverse  
> the results you expected from your method.
> 
> ## HTH, Sebastian
> 
> NP: Obituary - I’m in Pain  
> Jabber: removed\_email\_address@domain.invalid  
> ICQ: 205544826

Thanks, Sebastian!

I stupidly failed to recognize that “reverse” would affect each  
iteration of the recursive routine, rather than merely to last  
iteration as I intended.

I removed the interior “reverse” and the world is beautiful again, to  
wit:

parents(Class).reverse.inspect  
=\> [Class, Module, Object]

> TheClass.ancestors?

I did know about that, but thanks for asking. I wanted to write my  
own trace routine so that I have my own insight into metaprogramming  
techniques and the structure of Ruby.

Best wishes,  
Richard
