#extend_object only for #extend and not #include

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.

···

--
(-, /\ \/ / /\/

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

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

end

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

Guy Decoux

"Gavin Kistner" <gavin@refinery.com> schrieb im Newsbeitrag
news:8529C86C-2430-11D9-A186-000A959CF5AC@refinery.com...

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

"Gavin Kistner" <gavin@refinery.com> schrieb im Newsbeitrag
news:8529C86C-2430-11D9-A186-000A959CF5AC@refinery.com...

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! :slight_smile:

···

On Oct 22, 2004, at 8:09 AM, Robert Klemme wrote:

Just what the doctor ordered. Thanks, Guy! :slight_smile:

···

On Oct 22, 2004, at 7:50 AM, ts wrote:

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

"Gavin Kistner" <gavin@refinery.com> schrieb im Newsbeitrag news:DB3F483A-2483-11D9-845E-000A959CF5AC@refinery.com...

"Gavin Kistner" <gavin@refinery.com> schrieb im Newsbeitrag
news:8529C86C-2430-11D9-A186-000A959CF5AC@refinery.com...

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! :slight_smile:

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. :slight_smile:

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

Kind regards

    robert

···

On Oct 22, 2004, at 8:09 AM, Robert Klemme wrote: