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.
| Class | Name | corresponds to |
|---|---|---|
| Module | included | |
| Module | extended | |
| Module | append_feature | include? |
| Module | const_missing | const_defined? |
| Module | method_added | |
| Module | method_removed | |
| Module | method_undefined | |
| Class | inherited | |
| Object | singleton_method_added | |
| Object | singleton_method_removed | |
| Object | singleton_method_undefined | |
| Kernel | at_exit | |
| Kernel | method_missing | respond_to? |
| Kernel | set_trace_func | |
| Kernel | trace_var | |
| Kernel | untrace_var | |
| ObjectSpace | each_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.
| Class | Name | include super |
|---|---|---|
| Module | ancestors | |
| Module | constants | |
| Module | included_modules | |
| Module | instance_methods | true |
| Module | private_instance_methods | true |
| Module | protected_instance_methods | true |
| Module | public_instance_methods | true |
| Module | class_variables | |
| Class | superclass | |
| Object | methods | |
| Object | private_methods | true |
| Object | protected_methods | true |
| Object | public_methods | true |
| Object | singleton_methods | true |
| Object | instance_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.