Skip to content

Instantly share code, notes, and snippets.

@leopoldodonnell
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leopoldodonnell/c036b6356d6bb5a60400 to your computer and use it in GitHub Desktop.
Save leopoldodonnell/c036b6356d6bb5a60400 to your computer and use it in GitHub Desktop.
Monkey Patch a class without polluting the namespace and methods in a class
class ClassToMonkeyPatch
old_method = instance_method(:patched_method)
define_method(:patched_method) do |arg1, arg2, &block|
do_some_stuff
# delegate the rest to the old version of the method
res = old_method.bind(self).(arg1, arg2, &block)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment