1.2 Track Them Losses

Knowing when methods are removed

Both method_added and singleton_method_added have equivalent methods to track when methods are removed using Module#remove_method.

They're called - you guessed it - method_removed and singleton_method_removed respectively.

As always, the best way to learn is through exercises, so lets do a couple of those.

Hint

This is identical to the exercise for method_added - hook into the lifecycle with method_removed, then store the name of the method being removed in @@methods_removed

Output Window

Great! Now for singleton_method_removed.

Hint

This is identical to the exercise for singleton_method_added - hook into the lifecycle with method_removed, then store the name of the method being removed in @@singleton_methods_removed

Output Window

As with method_added and singleton_method_added, method_removed is defined in Module and singleton_method_removed in BasicObject.

Undefined Methods

Ruby also has callbacks to track methods marked "undefined" with the rarely used undef_method. We'll cover it for completeness, but don't expect to see or use this very often.

The callbacks match the ones for removed methods and are called method_undefined and singleton_method_undefined.

Output Window

Output Window

Congratulations, guest!


% of the book completed

or

This lesson is Copyright © 2011-2024 by Sidu Ponnappa and Jasim A Basheer