Question

If your coworker "opens" ("monkeypatches") a class in Ruby and redefines some important functionality that you need to use, how do you access that original pre-monkeypatched functionality without breaking a system that already relies/has dependencies on his monkeypatched definitions?

Was it helpful?

Solution

Given the example of the method overriding, if you can get some code loaded before his monkey patch is loaded then you can alias the method.

class Fixnum
  alias_method :original_plus, :+
end

class Fixnum
  def +(x)
    self - x
  end
end

>> 5 + 3
=> 2
>> 5.original_plus(3)
=> 8

OTHER TIPS

I recently saw this in the rubyflow feed - its a simple library that lets you namespace top level constants called aikidoka. Without any details of how/what is being monkey patched it is a bit tough to help. In theory though you could use an approach like this to namespace the monkey-patched version of the class so that you can access both it and the original independently.

Depends exactly what functionality was changed and in what way, but something implementing like Jim Wienrich's BlankSlate class may help:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top