method_added for class methods

C

Caio Chassot

Hi all,

Is there any way to get method_added to work with class methods? (or to
get the same functionality trough another means?)

Any pointers are appreciated.
 
T

Trans

class X
class << self
def method_added(*args)
p args
end
def singletin_method_added(*args)
p args
end
end
end

Don't ask me to explain it --it's mind numbing.

T.
 
D

David A. Black

Hi --

class X
class << self
def method_added(*args)
p args
end
def singletin_method_added(*args)

s/tin/ton/ :)
p args
end
end
end

I believe that only the singleton_method_added actually does anything.
I guess that's because the only methods you can add to this class are
singletons, and that gets priority.

At least, when I tried method_added, I couldn't get any output.
Don't ask me to explain it --it's mind numbing.

Like so many things, it comes down to:

* objects can have singleton methods
* singleton methods are stored in the object's singleton class
* classes are objects

It's so lovely, don't you think? :)


David
 
C

Caio Chassot

def singletin_method_added(*args)
s/tin/ton/ :)

Thanks. That did the trick. I didn't know about singleton_method_added.

Like so many things, it comes down to:

* objects can have singleton methods
* singleton methods are stored in the object's singleton class
* classes are objects

It's so lovely, don't you think? :)

Indeed :)
 
C

Caio Chassot

Thanks. That did the trick. I didn't know about singleton_method_added.

So I just bought the pickaxe 2 and it's there. Gotta love this book.

I was surprised I didn't find it in ri, but now I see it there and it's
a method of Object, not Module or Class. (which actually makes sense,
when you think about it for a second)
 
R

Robert Klemme

David A. Black said:
Hi --



s/tin/ton/ :)


I believe that only the singleton_method_added actually does anything.
I guess that's because the only methods you can add to this class are
singletons, and that gets priority.

At least, when I tried method_added, I couldn't get any output.
Worksforme:
:x
=> nil
Like so many things, it comes down to:

* objects can have singleton methods
* singleton methods are stored in the object's singleton class
* classes are objects

It's so lovely, don't you think? :)

Yes!

Kind regards

robert
 
D

David A. Black

Hi --

:x
=> nil

Right -- actually the one that I expected would work, but didn't,
was a different one, and I got my wires crossed. Here it is:

class C
class << self
def self.method_added(m)
p m
end
def x
end
end
end


David
 
C

Caio Chassot

Right -- actually the one that I expected would work, but didn't,
was a different one, and I got my wires crossed. Here it is:

class C
class << self
def self.method_added(m)
p m
end
def x
end
end
end

That's what I'd been trying before I posted.

Can anyone explain why it doesn't work?
 

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

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top