[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

#extend_object only for #extend and not #include

Gavin Kistner

10/22/2004 1:45:00 PM

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.
--
(-, /\ \/ / /\/



5 Answers

ts

10/22/2004 1:50:00 PM

0

>>>>> "G" == Gavin Kistner <gavin@refinery.com> 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




Robert Klemme

10/22/2004 2:07:00 PM

0


"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

10/22/2004 11:41:00 PM

0

On Oct 22, 2004, at 8:09 AM, Robert Klemme wrote:
> "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! :)



Gavin Kistner

10/22/2004 11:43:00 PM

0

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



Robert Klemme

10/23/2004 10:03:00 AM

0


"Gavin Kistner" <gavin@refinery.com> schrieb im Newsbeitrag
news:DB3F483A-2483-11D9-845E-000A959CF5AC@refinery.com...
> On Oct 22, 2004, at 8:09 AM, Robert Klemme wrote:
>> "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! :)

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