From: Rick DeNatale Date: 2009-09-01T07:11:17+09:00 Subject: [ruby-core:25237] Re: Module#prepend and Array#prepend On Mon, Aug 31, 2009 at 2:24 PM, Yusuke ENDOH wrote: > Hi, > > 2009/9/1 Carl Lerche : > >> This causes pain when you are using a ruby library that does not >> assume that you will be extending classes. Rails "solved" this problem using >> alias_method_chain, but this method can cause confusion. > ... > > Then, I guess that what we really need is a method wrapping feature: > > �class Person > � �refine_method(:speak) do |old_method, words| > � � �# old_method is a Method of original definition > � � �old_method.call("#{ words }!") > � �end > �end > �Person.new.speak("matz") #=> "matz!" Yusuke-san, Are you familiar with alias_method_chain in Rails which Carl mentioned. This is a common rails extension idiom which you'd use like this module Exclaimer def speak_with_exclaimer(words) speak_with_exclaimer("#{words}!") end def.self.included(base) base.alias_method_chain :speak, :exclaimer end end and alias_method_chain aliases the existing speak method to send_without_exclaimer, and then the speak_with_exclaimer method to speak This allows independently developed class extension modules to be composed, but it can be a bit confusing at times, particularly when debugging because of the aliasing. -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale