Finding parent-class hierarchy recursively

Discussion in 'Ruby' started by RichardOnRails, Sep 13, 2008.

  1. 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
     
    RichardOnRails, Sep 13, 2008
    #1
    1. Advertising

  2. 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
     
    Sebastian Hungerecker, Sep 13, 2008
    #2
    1. Advertising

  3. On Sep 13, 5:21 am, Sebastian Hungerecker <>
    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:
    > 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
     
    RichardOnRails, Sep 14, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. H.MuthuKumaraRajan
    Replies:
    3
    Views:
    493
    H.MuthuKumaraRajan
    Feb 4, 2004
  2. Replies:
    3
    Views:
    421
  3. seanacais
    Replies:
    1
    Views:
    331
    Bruno Desthuilliers
    Sep 9, 2008
  4. metal
    Replies:
    8
    Views:
    530
    Bruno Desthuilliers
    Oct 30, 2009
  5. Feyruz
    Replies:
    15
    Views:
    230
    Tad McClellan
    Nov 30, 2005
Loading...

Share This Page