In ruby the term monkey patch was misunderstood to mean any dynamic modification to a class and is often used as a synonym for dynamically modifying any class at runtime.
Ruby monkey patch class method.
You have to know where your monkey patches live.
I mostly follow rails monkey patching convention.
Due to ruby s open classes you can redefine or add functionality to existing classes.
Motivated by the intent to patch existing third party code as a workaround to a bug or feature.
Unfortunately the scope of such changes is global.
3 aspects of this code are likely to change in the next release.
Patches go into lib core.
The above statement asserts that the ruby usage is incorrect but terms evolve and that s not always a bad thing.
So the behavior of the class monkey is changed dynamically.
In the above class you are changing the behavior of the class method defined in the same program file.
2 monkey patched java methods are only available in ruby code.
When you monkey patch core classes you add to the core ruby apis.
For example you often see monkey patches that simply add a convenience method that has no side effect.
This can cause unintended side effects or breakage of programs.
The linking of a new method to the existing class method name happens at runtime.
So you have to have a way to quickly learn those changes when you jump into a new codebase.
We are assigning a new method name patchfunc to the existing class method name monkeyfunc.
In ruby a monkey patch mp is any dynamic modification to a class and is often used as a synonym for dynamically modifying any class add new or overwrite existing methods at runtime.
Or perhaps you need to monkey patch a class you own.
Every app with core patches feels a little bit different.
These classes methods are implemented in c and there are certain abstractions sacrificed in order to make the implementation easier and more efficient.
With that in mind let s look at a completely contrived example.
42 print x bar but you can t do this for certain built in classes like int or float.
Intro to monkey patching in ruby.
This is called a monkey patch.
There s no good reason to do this sort of patching because jruby marshals strings between java and ruby.
All users of the monkey patched class see the same changes.