views:

172

answers:

3

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?

A: 

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

ennuikiller
+3  A: 

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.

paulthenerd
Doesn't this require you to refactor your codebase to use the new name - Twitter::Mash instead of Mash?
pez_dispenser
You will almost certainly have to refer to one of the versions by a different name even in the alias_method case answered above since you are defining the same functionality in 2 different ways
paulthenerd
+4  A: 

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
Ian Terrell
You could put your code in the same file directly before your coworker's monkeypatch, if you have access to the source code. Otherwise the answer will depend on how your program is being loaded.
Ian Terrell
This is as close to a solution as I could imagine but I'm not sure how I could influence the order in which code is loaded. How could I insist on aliases being loaded before his monkey patches - simply by doing what you did here? Namely, inserting definitions at prior points in the source code - is that really all it takes?
pez_dispenser
Assuming you have access to his part of the codebase as well, just stick the alias in literally right above his monkeypatch.
Chuck
Except for a REST scenario, wouldn't you always have access to the source in a Ruby cowork situation?
pez_dispenser