[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. 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. iTooo
    Replies:
    4
    Views:
    537
    Peter_Julian
    Nov 5, 2005
  2. Angus
    Replies:
    5
    Views:
    1,482
    Krice
    Apr 10, 2008
  3. 7stud --
    Replies:
    11
    Views:
    465
    7stud --
    Nov 9, 2007
  4. David Mark
    Replies:
    16
    Views:
    939
    Scott Sauyet
    Nov 11, 2011
  5. David Mark
    Replies:
    58
    Views:
    1,525
    David Mark
    Dec 6, 2011
Loading...

Share This Page