Posted about 1 month ago. Visible to the public.

Call original method when monkey patching

Ruby offers monkey patching methods in order to change the behavior of a library if there's no better way.

We can call the method we're overriding inside our monkey patch:

class Foo def bar(argument) 'Hello' + argument end end module FooExtensions def bar super(' in my') + ' World' end end class Foo prepend FooExtensions # the only change to above: prepend instead of include end # => 'Hello in my World'

As mentioned, monkey patches are usually a threat to your code quality, so try to avoid them if possible.

Further readings

For a better understanding have a look at this article.

Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for old versions of Ruby on Rails (3.2 and 2.3).

Owner of this card:

Jakob Scholz
Last edit:
about 1 month ago
by Tobias Kraze
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Jakob Scholz to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more