List of callback methods

Posted by Gregor Schmidt
(2007-03-18)

If you ever wondered about the power of method_missing you were still missing a lot. Ruby has a whole bunch of meta callback methods. Since I always forget some of them, I will try to collect all of them in the following list, so everybody may benefit. If I missed something, please give me a hint.

ClassNamecorresponds to
Moduleincluded
Moduleextended
Moduleappend_featureinclude?
Moduleconst_missingconst_defined?
Modulemethod_added
Modulemethod_removed
Modulemethod_undefined
Classinherited
Objectsingleton_method_added
Objectsingleton_method_removed
Objectsingleton_method_undefined
Kernelat_exit
Kernelmethod_missingrespond_to?
Kernelset_trace_func
Kerneltrace_var
Kerneluntrace_var
ObjectSpaceeach_object

Most of this list is taken from this discussion on ruby-talk.

And of course there are all these nice collections, you may request.

ClassNameinclude super
Moduleancestors
Moduleconstants
Moduleincluded_modules
Moduleinstance_methodstrue
Moduleprivate_instance_methodstrue
Moduleprotected_instance_methodstrue
Modulepublic_instance_methodstrue
Moduleclass_variables
Classsuperclass
Objectmethods
Objectprivate_methodstrue
Objectprotected_methodstrue
Objectpublic_methodstrue
Objectsingleton_methodstrue
Objectinstance_variables

Please note, that most of the *methods have a parameter, which allows you to exclude inherited properties from the answer (as listed in the table).

Every ambitous Ruby programmer should have a look at these and come back each one or two months. These methods will allow you some shortcuts and more elegant solutions. As well, make sure, that these still work, after you introduced changes on the meta-level.