Redefining methods
Let's clear up some concepts before we move on. Redefining a method involves simply replacing one method with another. The original method is simply... lost.
Here's an example where you can try it out. You've probably already solved this as an exercise - now you get to break it.
Since almost every method in Ruby can be redefined, great care must be taken especially with core Ruby classes like Object
, Array
and so on. A thoughtless method redefinition can break the language entirely. A good rule of thumb is "Never redefine methods, ever, especially with classes supplied by the language."
Now that I've told you how dangerous this is, we both know you want to try it out. Let's get it out of your system right here where it won't hurt anyone. As an exercise, let's break addition for integers (well, Fixnum
s to be precise).
As always, make the tests pass. Please don't try this at home, and especially not at work.