Monkey Patching

In addition to extending any class you’d like, there’s a common practice in Ruby called Monkey Patching. Usually to overwrite functionality in a class, we’d have to extend the original class and override the public or protected method:

classCat{publicfunctionmeow(){echo'meow';}}// Now I'm going to override the Cat's meow() methodclassHungryCatextendsCat{publicfunctionmeow(){echo'roarrrrr!';}}$cat=newHungryCat;$cat->meow();=>'roarrrrr!'

The other option is to use the Wrapper pattern and “wrap” the inside functionality. The big difference here is that we cannot override the wrapped classes function but we can extend it.