Open In App

Monkey Patching in Ruby

Last Updated : 03 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 break 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.

  1. If multiple libraries have the same method, the first one will get overwritten.
  2. If the class is not imported before the patch, it will lead to a redefinition of the class instead of patching it.
  3. All the patches are global in nature and can actually disrupt multiple libraries.
  4. 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 patching
puts "Before blocking reverse: " +
     "Geeks for Geeks".reverse
      
# Apply patching
class String
  def reverse
    "Reversing blocked!!"
  end
end
 
# After applying patching
puts "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 patching
hash = { "Geeks"=>"G",
         "for"=>"F",
         "geeks"=>"g" }
          
puts "Before blocking reverse: "
      hash.delete "for"
puts "Deleted 'for' key"
puts hash
 
# Apply patching
class Hash
  def delete(key)
    "Delete blocked!!"
  end
end
 
# After applying patching
hash = { "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. 


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads