Hello Ruby-maintainers,
currently the hooks method_added, method_removed, method_undefined are
*not* sent to singleton_classes. Instead, the corresponding singleton
method hooks are sent to the "attached" instance.
Example from eval.c:
if (FL_TEST(klass, FL_SINGLETON)) {
rb_funcall(rb_iv_get(klass, "__attached__"), singleton_added, 1,
ID2SYM(mid));
}
else {
rb_funcall(klass, added, 1, ID2SYM(mid));
}
Is there any chance to always execute the else clause, as in:
if (FL_TEST(klass, FL_SINGLETON)) {
rb_funcall(rb_iv_get(klass, "__attached__"), singleton_added, 1,
ID2SYM(mid));
}
rb_funcall(klass, added, 1, ID2SYM(mid));
With this change, both the singleton method hooks and the method hooks
would be called in the case of a singleton class.
The reason I'm asking: in a library I'm writing I'd like to be able to
catch *every* method redefinition for a given object. Something like
o = Object.new
def o.singleton_method_added( m )
puts "gotcha"
end
Currently, this works only as long as the singleton_method_added method
isn't overridden. If someone later redefines
def o.singleton_method_added( m )
puts "no more"
end
the original method isn't called anymore. Changing Ruby as proposed
would allow me to do
class << o
def method_added( m )
puts "gotcha"
end
end
and get notified even when singleton_method_added is redefined.
I could send patches for this change, if you like.
Regards,
Pit