#extend_object only for #extend and not #include

G

Gavin Kistner

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.
 
T

ts

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
 
R

Robert Klemme

Gavin Kistner said:
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
"Bar just included Foo"
=> Bar"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

"Derived just inherited Base"
=> nil


Kind regards

robert
 
G

Gavin Kistner

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! :)
 
R

Robert Klemme

Gavin Kistner said:
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.
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top