Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Finding parent-class hierarchy recursively

Reply
Thread Tools

Finding parent-class hierarchy recursively

 
 
RichardOnRails
Guest
Posts: n/a
 
      09-13-2008
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
 
Reply With Quote
 
 
 
 
Sebastian Hungerecker
Guest
Posts: n/a
 
      09-13-2008
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
--
NP: Obituary - I'm in Pain
Jabber:
ICQ: 205544826

 
Reply With Quote
 
 
 
 
RichardOnRails
Guest
Posts: n/a
 
      09-14-2008
On Sep 13, 5:21*am, Sebastian Hungerecker <sep...@googlemail.com>
wrote:
> 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
> --
> NP: Obituary - I'm in Pain
> Jabber: sep...@jabber.org
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding Server... Finding Host.... enough already!!! Leesa_Tay@softhome.net Computer Support 2 01-20-2006 10:23 AM
finding maximum element in an array recursively Feyruz Perl Misc 15 11-30-2005 09:49 PM
Sorting bookmarks recursively? taras.di@gmail.com Firefox 0 07-31-2005 06:02 AM
Best way to recursively populate a tree based on SQL Server 2000 backend Mark ASP .Net 9 05-28-2005 08:54 PM
concatenate files recursively in win32 Mike James Perl 1 09-19-2003 03:51 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57