# Monkey patches to a method that exists in a plugin

**URL:** <https://www.ruby-forum.com/t/monkey-patches-to-a-method-that-exists-in-a-plugin/157226>\
**Category:** Rails\
**Created:** [January 8, 2009, 5:38pm UTC](https://www.ruby-forum.com/t/monkey-patches-to-a-method-that-exists-in-a-plugin/157226 "2009-01-08T17:38:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 8, 2009, 5:38pm UTC](https://www.ruby-forum.com/t/monkey-patches-to-a-method-that-exists-in-a-plugin/157226/1 "2009-01-08T17:38:01Z")

</div>

Say I have a plugin like so:

class Plugin  
def do\_something  
do\_thing :one  
do\_something\_else  
do\_thing :two  
end  
end

If I want to wrap the call to do\_something\_else in a begin/rescue  
block, then it would seem that I need to either hack the plugin in  
vendor/ or put a patch in lib/ that copies a portion of do\_something.  
Either way, it seems I lose on maintainability.

Is there a convention/method for monkey patching existing methods in  
plugins in these situations?

Thanks,  
Tom M.klin

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 8, 2009, 6:08pm UTC](https://www.ruby-forum.com/t/monkey-patches-to-a-method-that-exists-in-a-plugin/157226/2 "2009-01-08T18:08:01Z")

</div>

On 8 Jan 2009, at 16:37, Tom M wrote:

> If I want to wrap the call to do\_something\_else in a begin/rescue  
> block, then it would seem that I need to either hack the plugin in  
> vendor/ or put a patch in lib/ that copies a portion of do\_something.  
> Either way, it seems I lose on maintainability.

The mighty alias\_method\_chain (which is really just 2 calls to  
alias\_method) can work here

class Plugin  
def do\_something\_else\_with\_rescue  
begin  
do\_something\_else\_without\_rescue  
rescue …  
end  
end  
alias\_method\_chain :do\_something\_else, :rescue  
end
