Hooking Class Methods

T

Trans

This works:

module PromoteSelf
def method_added( *args )
p args
end
end

module String::Format
extend PromoteSelf

def jumble( obj, x, y )
p obj+x+y
end
end

But NOT this:

module PromoteSelf
def method_added( *args )
p args
end
end

module String::Format
class << self
extend PromoteSelf

def jumble( obj, x, y )
p obj+x+y
end
end
end

How do I hook in module/class method creation?

Thanks,
T.
 
T

Trans

To answer my own question:

module PromoteSelf
def singleton_method_added( *args )
p args
end
end

module String::Format
class << self
include PromoteSelf

def jumble( obj, x, y )
p obj+x+y
end
end
end

But I'll be honest with you. I find it odd. Why would'nt the later
work? It almost seems like it would have to be purposfully disallowed.

Nonetheless, now that I've got this working I have something neat to
show ...
 
C

Charles Steinman

Trans said:
To answer my own question:

module PromoteSelf
def singleton_method_added( *args )
p args
end
end

module String::Format
class << self
include PromoteSelf

def jumble( obj, x, y )
p obj+x+y
end
end
end

But I'll be honest with you. I find it odd. Why would'nt the later
work? It almost seems like it would have to be purposfully disallowed.

Module#method_added is called when a new method is added to the
module's collection of methods. When you define a method as
SomeModule.method_name, you're not adding a method to the module's
collection, you're adding a singleton method to the SomeModule object.
You could do the same for any object, which is why Object contains
singleton_method_added.
 
T

Trans

Charles said:
Module#method_added is called when a new method is added to the
module's collection of methods. When you define a method as
SomeModule.method_name, you're not adding a method to the module's
collection, you're adding a singleton method to the SomeModule object.
You could do the same for any object, which is why Object contains
singleton_method_added.

Thanks. What I found strange was that

def self.method_added

is a singleton method too. So I thought it would hook singleton method
creation. I see now that this isn't the case b/c singletons "shadow"
the real methods. So this wouldn't work. Hence the seperate method.

T.
 
P

Pit Capitain

Trans said:
To answer my own question:

module PromoteSelf
def singleton_method_added( *args )
p args
end
end

module String::Format
class << self
include PromoteSelf

def jumble( obj, x, y )
p obj+x+y
end
end
end

But I'll be honest with you. I find it odd. Why would'nt the later
work? It almost seems like it would have to be purposfully disallowed.

Nonetheless, now that I've got this working I have something neat to
show ...

Compare [ruby-core:4855]. Matz thinks this would make singleton classes
more present in Ruby.

Regards,
Pit
 
T

Trans

Pit said:
Compare [ruby-core:4855]. Matz thinks this would make singleton classes
more present in Ruby.

Intersting, thanks. Erm... how much more "present" can you get when
every class method is one? Besides what's so bad about singletons? Hek,
I think they should be layerable.

T.
 
P

Pit Capitain

Trans said:
Pit said:
Compare [ruby-core:4855]. Matz thinks this would make singleton classes
more present in Ruby.

Intersting, thanks. Erm... how much more "present" can you get when
every class method is one? Besides what's so bad about singletons? Hek,
I think they should be layerable.

As I understand it, Matz wanted to have singleton methods in Ruby, but
not necessarily singleton classes. He has seen singleton classes as a
feature of the current implementation, not as part of the "official"
language. IIRC, this could change in the future. I'm waiting...

Regards,
Pit
 

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,773
Messages
2,569,594
Members
45,125
Latest member
VinayKumar Nevatia_
Top