Ruby lets you hook in and see (and change!) a lot of behavior of the core language. Methods, constants, classes, variables… Ruby lets you query them all, and change a lot about them.
These are just hooks – things Ruby calls in response to something happening. That’s different from, say, all the methods you can call to find our what’s defined and how – like instance variables, or method bindings, or…
Here’s summaries and links for all the hooks I could find (thanks to Google and StackOverflow!):
Methods
- respond_to_missing?: a way to make sure your dynamic methods defined with method_missing also handle respond_to? correctly
- method_missing: called when a method cannot be found, potentially to allow dynamically defining one instead
- method_added: called whenever a method is added… which can be used to modify the method
- method_removed: called whenever a method is removed
- singleton_method_added: method added to the singleton class of the object, to be callable just on this one instance
- singleton_method_removed: method removed from singleton class
- method_undefined: a method has been undefined, with undef_method. Undef_method is different from remove_method because remove_method may still allow superclasses to define the method – undef_method means it’s gone entirely. singleton_method_undefined: called when a singleton method is undefined entirely
- initialize_copy: an optional callback when cloning any Object
Classes
Modules
- append_features: a Module is included, and its constants, methods and variables used
- included: a Module is included, which usually obsoletes “append_features”
- extend_object: a Module extends an Object
- extended: an Object is extended by a module, which mostly obsoletes extend_object
- const_missing: a constant isn’t already present
Marshalling
- marshal_dump: called on an object to have it dump itself in Ruby Marshal format
- marshal_load: called on an object to have it load itself in Ruby Mashal format
Coercion
- coerce: called by the first type in a two-argument operator on the second argument, to make it turn into something the first argument can recognize
- induced_from: deprecated, please don’t use
- to_i, to_f, to_s, to_a, to_hash, to_proc and others: conversions, indicating that the object is being used as a type and should try to convert itself to that type
Comments