Approaches to Class Method Monkeypatching in Ruby
Monkeypatching in Ruby lets you modify or extend a class's behavior at runtime. For class methods—methods called directly on the class rather than its instances—there are several ways to weave in new functionality. Throughout, we'll patch a MyPlrClass whose original_method returns "Original". Each technique works, but the trade-offs differ.
The simplest approach is to reopen the class and redefine the method: declare the class again and provide a new def self.original_method. After that, the method returns "Patched". This is clean but ruthless—it erases the original method unless you've saved its logic elsewhere.
class MyPlrClass
def self.original_method
"Original"
end
end
class MyPlrClass
def self.original_method
"Patched"
end
end
puts MyPlrClass.original_method # => "Patched"
For a programmatic variant, class_eval lets you redefine the method dynamically, slipping a new instruction into the class's machinery without fully reopening it. Call MyPlrClass.class_eval, define the method, and it now returns "Patched via class_eval". This suits scripted, on-the-fly changes, but like reopening, it overwrites the original.
class MyPlrClass
def self.original_method
"Original"
end
end
MyPlrClass.class_eval do
def self.original_method
"Patched via class_eval"
end
end
puts MyPlrClass.original_method # => "Patched via class_eval"
To keep the original behavior, use aliasing. With alias_method, you rename the original to something like old_original_method before redefining original_method, so the new method can call the original and blend old and new.
class MyPlrClass
def self.original_method
"Original"
end
end
class MyPlrClass
class << self
alias_method :old_original_method, :original_method
def original_method
"Patched, but calling original: #{old_original_method}"
end
end
end
puts MyPlrClass.original_method # => "Patched, but calling original: Original"
A more dynamic option is define_singleton_method, which crafts a class method directly on the class's singleton class. Call define_singleton_method(:original_method) with a block, and it takes over, returning "Patched via define_singleton_method". It's elegant for one-off changes but, without aliasing, discards the original method.
class MyPlrClass
def self.original_method
"Original"
end
end
MyPlrClass.define_singleton_method(:original_method) do
"Patched via define_singleton_method"
end
puts MyPlrClass.original_method # => "Patched via define_singleton_method"
For reusability, prepend a module. Define your patched method in a module, then use prepend on the class's singleton class. Ruby's method lookup prioritizes the module, and you can still call the original via super—a layer that enhances without destroying the original.
class MyPlrClass
def self.original_method
"Original"
end
end
module Patch
def original_method
"Patched via prepend, original: #{super}"
end
end
MyPlrClass.singleton_class.prepend(Patch)
puts MyPlrClass.original_method # => "Patched via prepend, original: Original"
Alternatively, extend the class with a module using extend: define your method in a module, then call MyPlrClass.extend(Patch). The method is replaced, but the module keeps your patch organized and reusable across classes.
class MyPlrClass
def self.original_method
"Original"
end
end
module Patch
def original_method
"Patched via extend"
end
end
MyPlrClass.extend(Patch)
puts MyPlrClass.original_method # => "Patched via extend"
For metaprogramming enthusiasts, the singleton_method_added hook intercepts method definitions. Set it up in MyPlrClass to detect when original_method is defined, alias it, and redefine it to include the original—a powerful but complex trick that patches automatically.
class MyPlrClass
def self.singleton_method_added(method_name)
if method_name == :original_method
alias_method :old_original_method, :original_method
define_singleton_method(:original_method) do
"Patched via hook, original: #{old_original_method}"
end
end
end
def self.original_method
"Original"
end
end
puts MyPlrClass.original_method # => "Patched via hook, original: Original"
The safer alternative is refinements. Instead of globally altering MyPlrClass, define a refinement in a module and activate it with using. The patch then applies only in specific scopes, leaving the class untouched elsewhere.
module Patch
refine MyPlrClass.singleton_class do
def original_method
"Patched via refine"
end
end
end
class MyPlrClass
def self.original_method
"Original"
end
end
using Patch
puts MyPlrClass.original_method # => "Patched via refine"
puts MyPlrClass.original_method # => "Original" (outside `using` scope)
Each approach has its place. Reopening or extending is quick for small projects; aliasing and prepending preserve the original method for cautious tweaks; refinements shine in larger codebases where global changes risk chaos. Whichever you choose, test thoroughly—monkeypatching is powerful and can break existing code.