[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

define method hook

Ben Burkert

9/14/2007 9:48:00 PM

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

3 Answers

Wilson Bilkovich

9/14/2007 9:54:00 PM

0

On 9/14/07, Ben Burkert <ben@benburkert.com> wrote:
> 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

Erik Veenstra

9/14/2007 9:57:00 PM

0

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


Ben Burkert

9/14/2007 9:59:00 PM

0

great, thanks.

On Sep 14, 2007, at 4:54 PM, Wilson Bilkovich wrote:

> On 9/14/07, Ben Burkert <ben@benburkert.com> wrote:
>> 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
>