define method hook

B

Ben Burkert

Is there a way to intercept method defines, so that i can setup a
callback whenever a new method is defined for a class? I'm looking
for something that works with the def keyword, and doesn't require
any extensions.

-Ben
 
W

Wilson Bilkovich

Is there a way to intercept method defines, so that i can setup a
callback whenever a new method is defined for a class? I'm looking
for something that works with the def keyword, and doesn't require
any extensions.

Yep. Module#method_added
If you do this:

class Example
def self.method_added(meth)
p meth
end
end

After that, this code:
class Example;def foo(baz);5;end;end

..will print out: :foo
 
E

Erik Veenstra

Is there a way to intercept method defines, so that i can setup a
callback whenever a new method is defined for a class?

class Module
def method_added(method_name)
p [caller[0], self, method_name]
end
end

class Foo
def bar
end
end

gegroet,
Erik V.
 
B

Ben Burkert

great, thanks.

Yep. Module#method_added
If you do this:

class Example
def self.method_added(meth)
p meth
end
end

After that, this code:
class Example;def foo(baz);5;end;end

..will print out: :foo
 

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

No members online now.

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,537
Latest member
BellCorone

Latest Threads

Top