Monkey Patching in Ruby - GeeksforGeeks

22 Oct, 2020
In Ruby, a Monkey Patch (MP) is referred to as a dynamic modification to a class and by a dynamic modification to a class means to add new or overwrite existing methods at runtime. This ability is provided by ruby to give more flexibility to the coders.
Ruby, being a very powerful and agile language, provides this extreme power to the developer of patching a class. In real the importance of it is to patch a buggy class and make its method behave in a manner to solve the purpose. As said with great power comes great responsibilities, it’s a big responsibility on the developer to use this feature to patch the class rather than to brake its original functionality as patching leads to overriding of the original methods. 
It is a very important feature and needs extra care while using it. There are some basic properties for monkey patching in ruby listed as follows.
If multiple libraries have the same method, the first one will get overwritten.If the class is not imported before the patch, it will lead to a redefinition of the class instead of patching it.All the patches are global in nature and can actually disrupt multiple libraries.Monkey patching is used to patch up classes that are owned by the coder and it’s not recommended to patch a class already defined in Ruby which are used frequently like Hashes, Lists, etc.
If multiple libraries have the same method, the first one will get overwritten.
If the class is not imported before the patch, it will lead to a redefinition of the class instead of patching it.
All the patches are global in nature and can actually disrupt multiple libraries.
Monkey patching is used to patch up classes that are owned by the coder and it’s not recommended to patch a class already defined in Ruby which are used frequently like Hashes, Lists, etc.
The general syntax for applying a patch is to simply make a method inside a class, having a class name same as that on which patch has to be applied.  Syntax  
class [class_name]
     def [method_to_patch]:
       #do_something
     end
end 

Example:In this example, Monkey patching is used to block the user to reverse the string.
Ruby
# Ruby program to illustrate monkey patching  # Before applying patchingputs "Before blocking reverse: " +     "Geeks for Geeks".reverse       # Apply patchingclass String  def reverse    "Reversing blocked!!"  endend  # After applying patchingputs "After blocking reverse: " +      "Geeks for Geeks".reverse
Output:
Before blocking reverse: skeeG rof skeeG
After blocking reverse: Reversing blocked!!

In the above code, the String class of the ruby is altered in order to block the functionality of reversing the string. After defining the method for the patch, the patch actually blocks a basic functionality of the class, thus to be used with care. 
Example:In this example, Monkey patching is used to block the user to delete any key from Hash. 
Ruby
# Ruby program to illustrate monkey patching  # Before applying patchinghash = { "Geeks"=>"G",         "for"=>"F",         "geeks"=>"g" }           puts "Before blocking reverse: "      hash.delete "for"puts "Deleted 'for' key"puts hash  # Apply patchingclass Hash  def delete(key)    "Delete blocked!!"  endend  # After applying patchinghash = { "Geeks"=>"G",         "for"=>"F",         "geeks"=>"g" }           puts "Before blocking reverse: "puts "Deleting 'for' key but " +      hash.delete("for")puts hash
Output:
Before blocking reverse: 
Deleted 'for' key
{"Geeks"=>"G", "geeks"=>"g"}
Before blocking reverse: 
Deleting 'for' key but Delete blocked!!
{"Geeks"=>"G", "for"=>"F", "geeks"=>"g"}

Similar to the above code, here the deletion functionality is blocked due to the patch. 
Some basic tips on when to actually use monkey patch: 
Cases when reopening a class is required for example in the case of code refactoring and simplification when the method is written in a dirty manner inside the class.
When patching is required for a developer’s own class methods. It is not well recommended to use a monkey patch in such a case though. 
Picked
Ruby
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Ruby | Array reverse() function
Method Overloading In Ruby
Ruby | Array transpose() function
Instance Variables in Ruby
Ruby | Array class insert() function
Ruby Static Members
Ruby | Method overriding
Ruby | Array replace() function
Ruby | clear() function
Ruby | Array unshift() function