#extend_object only for #extend and not #include

Discussion in 'Ruby' started by Gavin Kistner, Oct 22, 2004.

  1. Just thought I'd point out my surprise that the Module#extend_object
    callback only fires when #extend is explicitly called, and not when the
    module extends the class via #include:

    module Foo
    def self.extend_object( o )
    p "#{o} just extended by #{self}"
    end
    end

    class Bar; include Foo; end
    class Bar2; self.extend( Foo ); end

    #=> "Bar2 just extended by Foo"


    It'd be nice, IMO, to have it work in both cases.
    --
    (-, /\ \/ / /\/
     
    Gavin Kistner, Oct 22, 2004
    #1
    1. Advertising

  2. Gavin Kistner

    ts Guest

    >>>>> "G" == Gavin Kistner <> writes:

    G> module Foo
    G> def self.extend_object( o )
    G> p "#{o} just extended by #{self}"
    G> end

    def self.append_features( o )
    p "#{o} just include #{self}"
    end

    G> end

    G> class Bar; include Foo; end
    G> class Bar2; self.extend( Foo ); end


    Guy Decoux
     
    ts, Oct 22, 2004
    #2
    1. Advertising

  3. "Gavin Kistner" <> schrieb im Newsbeitrag
    news:...
    > Just thought I'd point out my surprise that the Module#extend_object
    > callback only fires when #extend is explicitly called, and not when the
    > module extends the class via #include:
    >
    > module Foo
    > def self.extend_object( o )
    > p "#{o} just extended by #{self}"
    > end
    > end
    >
    > class Bar; include Foo; end
    > class Bar2; self.extend( Foo ); end
    >
    > #=> "Bar2 just extended by Foo"


    Well, that's quite logical, isn't it? I mean, include and extend are two
    different cups of tea.

    Apart from the solution Guy posted you can do this:

    module Foo
    def self.extend_object( o )
    p "#{o} just extended by #{self}"
    end

    def self.included( o )
    p "#{o} just included #{self}"
    end
    end

    >> class Bar; include Foo; end

    "Bar just included Foo"
    => Bar
    >> class Bar2; self.extend( Foo ); end

    "Bar2 just extended by Foo"
    => Bar2


    And for classes there is #inherited:

    class Base
    def self.inherited(cl)
    p "#{cl} just inherited #{self}"
    end
    end


    >> class Derived < Base ; end

    "Derived just inherited Base"
    => nil


    Kind regards

    robert
     
    Robert Klemme, Oct 22, 2004
    #3
  4. On Oct 22, 2004, at 8:09 AM, Robert Klemme wrote:
    > "Gavin Kistner" <> schrieb im Newsbeitrag
    > news:...
    >> Just thought I'd point out my surprise that the Module#extend_object
    >> callback only fires when #extend is explicitly called, and not when
    >> the
    >> module extends the class via #include:

    > Well, that's quite logical, isn't it? I mean, include and extend are
    > two
    > different cups of tea.


    Are they? Other than different receivers (and thus the ability for
    #extend to operate on instances) are they functionally different and
    I'm a moron for not realizing it?


    > def self.included( o )
    > p "#{o} just included #{self}"
    > end


    Apparently I didn't know about this. That'll do...thanks! :)
     
    Gavin Kistner, Oct 23, 2004
    #4
  5. On Oct 22, 2004, at 7:50 AM, ts wrote:
    > def self.append_features( o )
    > p "#{o} just include #{self}"
    > end


    Just what the doctor ordered. Thanks, Guy! :)
     
    Gavin Kistner, Oct 23, 2004
    #5
  6. "Gavin Kistner" <> schrieb im Newsbeitrag
    news:D...
    > On Oct 22, 2004, at 8:09 AM, Robert Klemme wrote:
    >> "Gavin Kistner" <> schrieb im Newsbeitrag
    >> news:...
    >>> Just thought I'd point out my surprise that the Module#extend_object
    >>> callback only fires when #extend is explicitly called, and not when the
    >>> module extends the class via #include:

    >> Well, that's quite logical, isn't it? I mean, include and extend are two
    >> different cups of tea.

    >
    > Are they? Other than different receivers (and thus the ability for #extend
    > to operate on instances) are they functionally different and I'm a moron
    > for not realizing it?


    Well, maybe I'm the moron. Lemmesee... I think, we're both right: include
    and extend are different because they have different receivers: include's
    receiver must be a Module (or sub class) while extend's receiver must be an
    instance. Behind the scenes of course, extend should do something like
    this - at least conceptually:

    def extend(x)
    class << self
    include x
    end
    end

    I guess the reason why we see differences here is the fact that singleton
    classes are handled specially and thus maybe don't invoke Module#included.

    >> def self.included( o )
    >> p "#{o} just included #{self}"
    >> end

    >
    > Apparently I didn't know about this. That'll do...thanks! :)


    So now you have two options: either recognize include and extend differently
    or treat them alike. I find this pretty complete: you got the choice. :)

    Btw: *I* didn't know about append_features - so we all have learned
    something. :)

    Kind regards

    robert
     
    Robert Klemme, Oct 23, 2004
    #6
    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. Andreas Bogenberger
    Replies:
    3
    Views:
    1,010
    Andreas Bogenberger
    Feb 22, 2008
  2. Simon Strandgaard

    proposal: class<<obj to invoke extend_object

    Simon Strandgaard, Mar 7, 2004, in forum: Ruby
    Replies:
    2
    Views:
    100
    Simon Strandgaard
    Mar 8, 2004
  3. Daniel Berger
    Replies:
    3
    Views:
    128
    Daniel Berger
    Oct 30, 2004
  4. Alexandru Popescu

    include and extend Module - Ruby Way

    Alexandru Popescu, Aug 21, 2005, in forum: Ruby
    Replies:
    9
    Views:
    144
    Devin Mullins
    Aug 22, 2005
  5. rlf
    Replies:
    1
    Views:
    97
    Gary Wright
    May 22, 2009
Loading...

Share This Page