From: David Alan Black Date: 2002-04-21T20:38:37+09:00 Subject: Re: problem with alias Hi -- On Sun, 21 Apr 2002, Guy N. Hurst wrote: > I just discovered a problem with how alias works while > testing some code in irb. > > It is common in ruby to alias a method you are about to > override so you can still access the original functionality. > However, if that portion of code is executed more than > once (such as being included or evaluated more than once), > then the original method gets lost. > > In short, > > alias old_meth meth > alias old_meth meth > > will result in meth being lost forever. [...] > Perhaps a companion to #send would work, such as #send_original ? > Yet, I suppose someone could override *that* and cause problems. > So it really does point to the possible need for a new keyword, if > not an alteration to the existing functionality of alias. Perhaps > adding a third parameter indicating whether the original or existing > method should be aliased? > > alias old_sf % true # true means use original > alias old_meth meth false # false means don't use original; this is default > alias old_meth meth # same as using false for third param > > Comments? I'm not sure the concept of an "original" definition is an exact fit for the problem. You might not always know what the first definition of something was, and/or the one you want to revert to might be an interim definition. For instance -- a very contrived example, but anyway -- let's say you write this: # thing.rb class Thing def action puts "original definition" end alias :oldaction :action true def action puts "second definition" puts "I used to do this: " oldaction end end and then someone else writes this: class Thing def action puts "sneaky pre-original definition" end end require 'thing' (Hey, I *said* it was contrived :-) David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav