Monkey Patch Programming Method and Its Application in Ruby, patchruby
What is Monkey Patch )? In dynamic languages, functions are appended and changed without modifying the source code.
Objective of using Monkey patch:
1. append Function
2. Function Change
3. modify program errors
4. Add hooks to execute some other processing operations while executing a method, such as printing logs and Implementing AOP,
5. cache: When the calculation amount is large and the settlement result can be used repeatedly, replacing the method after a computation can improve the processing speed.
Ruby classes are open classes, that is, you can add any content after the class definition, which makes it very easy to use Monkey patches in Ruby. In addition, Ruby provides methods, classes, and modules for operations, making it easier to use Monkey patches. Ruby provides the following basic functions:
Alias: alias A Method
Include: Method for introducing other modules
Remove_method: cancels the method in this class.
Undef: cancelling Method
Use Monkey Patch in Ruby
I met the following scenario:
We use a third-party database fog for EC2 operations. You need to set the instance type parameter for many commands such as instance creation. In fog, all EC2 types are defined in the flavors array of fog/aws/models/compute/FLAVORS. rb. If the set type is not in the FLAVORS array, the fog will regard it as an invalid parameter and report an error.
Later, Amazon released the new instance type D2. Although Ruby's third-party community is very active, fog's development community has not added D2 to flavors. rb in time. Our work urgently requires D2 instances.
After explaining the background, let's take a look at the solutions.
Method 1: We can submit a Pull Request to the fog to add a new type.
But this method does not work. The dependency on the fog version of The knife-ec2 we use must be 1.25. *, but the fog has been updated to 1.31.0, And the fog structure has changed significantly since 1.27.0. Apparently, we could not wait for the knife-ec2 upgrade to support the new version of the fog, So we submitted the Pull Request to update the fog, which could not solve the problem.
Method 2: manually update the old fog version. Since the latest fog version cannot be used, you can manually edit the fog version 1.25 and package it into a Gem. This method is easier to operate than the previous method, but it is difficult to maintain problems. For a minimal change, adding your code to a third-party library always makes people feel "not clean 」.
Finally, with my colleagues' guidance, I adopted the third method, Monkey Patch. I added a file lib/PROJECT_NAME/monkey_patches/flavors. rb to our Ruby project. Then I added the following code to the file to modify fog/aws/models/compute/flavors:
require 'fog/aws/models/compute/flavors'class Object def redef_without_warning(const, value) mod = self.is_a?(Module) ? self : self.class mod.send(:remove_const, const) if mod.const_defined?(const) mod.const_set(const, value) endendmodule Fog module Compute class AWS NEW_FLAVORS = FLAVORS + [ { :id => "d2.xlarge", ... }, { :id => "d2.2xlarge", ... }, { :id => "d2.4xlarge", ... }, { :id => "d2.8xlarge", ... } ] redef_without_warning :FLAVORS, NEW_FLAVORS end endend
Summary
By adding a Monkey patch in your code, we have successfully added the new instance type to the fog dynamically. At last, we can use fog to create machines of the D2 type. In addition, This method requires the smallest amount of code and is easier to maintain.
Monkey Patch is not a perfect solution. It introduces some traps. So this technique is still controversial in the field of software engineering. However, I still think Monkey Patch is a good zero-time solution.