Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > #extend_object only for #extend and not #include

Reply
Thread Tools

#extend_object only for #extend and not #include

 
 
Gavin Kistner
Guest
Posts: n/a
 
      10-22-2004
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.
--
(-, /\ \/ / /\/



 
Reply With Quote
 
 
 
 
ts
Guest
Posts: n/a
 
      10-22-2004
>>>>> "G" == Gavin Kistner <(E-Mail Removed)> 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




 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      10-22-2004

"Gavin Kistner" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> 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

 
Reply With Quote
 
Gavin Kistner
Guest
Posts: n/a
 
      10-22-2004
On Oct 22, 2004, at 8:09 AM, Robert Klemme wrote:
> "Gavin Kistner" <(E-Mail Removed)> schrieb im Newsbeitrag
> news:(E-Mail Removed)...
>> 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!



 
Reply With Quote
 
Gavin Kistner
Guest
Posts: n/a
 
      10-22-2004
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!



 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      10-23-2004

"Gavin Kistner" <(E-Mail Removed)> schrieb im Newsbeitrag
news(E-Mail Removed)...
> On Oct 22, 2004, at 8:09 AM, Robert Klemme wrote:
>> "Gavin Kistner" <(E-Mail Removed)> schrieb im Newsbeitrag
>> news:(E-Mail Removed)...
>>> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unicode thing that causes a traceback in 2.6 and 2.7 but not in 2.5,and only when writing to a pipe, not to the terminal Dan Stromberg Python 1 12-02-2010 05:03 PM
Read only and / or write only class members A C++ 1 11-03-2010 07:36 PM
matching only alphabetic chars and only digits with periods Jack Perl Misc 4 07-14-2006 10:22 PM
Database file and folder are not read only but error message says it is read only?? keithb ASP .Net 2 06-07-2006 03:40 PM
My boss so cheap!! Our ASP.NET only have 256MB only! How to release request used memory when Page Unload() event raise? ABC ASP .Net 7 01-13-2006 03:23 PM



Advertisments