[Tip] When is Class#inherited called?

Discussion in 'Ruby' started by Gavin Kistner, Sep 25, 2004.

  1. (resend because the initial message didn't seem to get through)

    I was wondering exactly when the #inherited method was called, and
    (lacking documentation) did a quick investigation. The answer is "as
    soon as the subclass begins being defined (the first time only) before
    the first line of code in the definition".

    (See that at the end, #inherited is not again when Foo::Bar is
    re-opened.)

    This is what I hoped, but not what I expected to find. Yay Ruby :)


    p 'Before Foo'
    class Foo
    def self.inherited( k )
    p "#{self}#inherited( #{k} )"
    end

    p "Inside #{self}, before Bar"
    class Bar < self
    p "Inside #{self}, before Jee"
    class Jee < self;
    p "Inside #{self}"
    end
    p "Inside #{self}, after Jee"
    end
    p "Inside #{self}, after Bar"
    end
    p 'After Foo'

    class Foo2 < Foo
    p "Inside #{self}"
    end

    class Foo::Bar < Foo
    p "Re-opened #{self}"
    end

    #=> "Before Foo"
    #=> "Inside Foo, before Bar"
    #=> "Foo#inherited( Foo::Bar )"
    #=> "Inside Foo::Bar, before Jee"
    #=> "Foo::Bar#inherited( Foo::Bar::Jee )"
    #=> "Inside Foo::Bar::Jee"
    #=> "Inside Foo::Bar, after Jee"
    #=> "Inside Foo, after Bar"
    #=> "After Foo"
    #=> "Foo#inherited( Foo2 )"
    #=> "Inside Foo2"
    #=> "Re-opened Foo::Bar"
    --
    (-, /\ \/ / /\/
     
    Gavin Kistner, Sep 25, 2004
    #1
    1. Advertisements

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. Steve Sobol
    Replies:
    3
    Views:
    719
    Roedy Green
    Aug 22, 2005
  2. E11
    Replies:
    1
    Views:
    5,756
    Thomas Weidenfeller
    Oct 12, 2005
  3. iTooo
    Replies:
    4
    Views:
    637
    Peter_Julian
    Nov 5, 2005
  4. Angus
    Replies:
    5
    Views:
    1,670
    Krice
    Apr 10, 2008
  5. Marc-andre Lafortune

    Inherited called at different times

    Marc-andre Lafortune, Jan 3, 2007, in forum: Ruby
    Replies:
    0
    Views:
    151
    Marc-andre Lafortune
    Jan 3, 2007
  6. 7stud --
    Replies:
    11
    Views:
    778
    7stud --
    Nov 9, 2007
  7. David Mark
    Replies:
    16
    Views:
    1,233
    Scott Sauyet
    Nov 11, 2011
  8. David Mark
    Replies:
    58
    Views:
    2,202
    David Mark
    Dec 6, 2011
Loading...