From: "Ondřej Bílka" Date: 2011-07-10T01:37:58+09:00 Subject: [ruby-core:37927] Re: [Ruby 1.9 - Feature #5005] Provide convenient access to original methods One of answers is use AOP like aquarium(But I didn't tried it so I dont know if it isn't overkill). Currently best solution is use unbound method which is not much intuitive From perspective that easiest solution should be too wrong we could introduce wrap_method. It avoids problems when alias_method break alias_method of previous guy. class Module def wrap_method(foo) i=instance_method(foo) define_method(foo){|*a| yield(i.bind(self),*a) } end end class A def foo 1 end wrap_method(:foo){|prev| prev.()+1 } end puts A.new.foo On Sat, Jul 09, 2011 at 11:27:46PM +0900, Yukihiro Matsumoto wrote: > > Issue #5005 has been updated by Yukihiro Matsumoto. > > > The idea to address the issue is the "prepend" feature planned for Ruby 2.0. > > class Original > def any_method > ... > end > end > > module Modifier > def any_method > #custom code > super # call the original any_method, passing *args > #custom code > end > end > > # reopen class > class Original > prepend Modifier > end > > The prepend method pushes a module in front of a class so that super calls the original method. > > matz. > > ---------------------------------------- > Feature #5005: Provide convenient access to original methods > http://redmine.ruby-lang.org/issues/5005 > > Author: Lazaridis Ilias > Status: Open > Priority: Normal > Assignee: > Category: core > Target version: 2.0 > > > The languag allows a class to be "reopened", thus it's behaviour can be redefined: > > class String > def any_method > #custom code > end > end > > the original method can be called, using this construct: > > class String > alias_method :original_any_method, :any_method > def any_method(*args) > #custom code > original_any_method(*args) > #custom code > end > end > > In order to make this more convenient, the following construct could be provided: > > class String > def any_method(*args) > #custom code > original # call the original String#any_method, passing *args (similar to "super") > #custom code > end > end > > "original" would behave similar to "super" > > The term "original" can be replaced by any term which describes this concept better. > > > > > > > -- > http://redmine.ruby-lang.org -- Your EMAIL is now being delivered by the USPS.