Forum: Ruby on Rails monkey patches to a method that exists in a plugin

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7877268675d21c33224951b247d518cf?d=identicon&s=25 Tom M (Guest)
on 2009-01-08 17:38
(Received via mailing list)
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 Macklin
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-01-08 18:08
(Received via mailing list)
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
This topic is locked and can not be replied to.