From: "Guy N. Hurst" Date: 2002-04-21T06:12:21+09:00 Subject: problem with alias 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. Below is an example: irb(main):001:0> "%s" % 5 "5" irb(main):002:0> class String irb(main):003:1> alias old_sf % irb(main):004:1* def %(other) irb(main):005:2> puts "dummy test:" irb(main):006:2> old_sf other irb(main):007:2> end irb(main):008:1> end nil irb(main):009:0> "%s" % 5 dummy test: "5" irb(main):010:0> "%s".old_sf 5 "5" irb(main):011:0> class String irb(main):012:1> alias old_sf % irb(main):013:1* def %(other) irb(main):014:2> puts "dummy test:" irb(main):015:2> old_sf other irb(main):016:2> end irb(main):017:1> end nil irb(main):018:0> "%s" % 5 dummy test: dummy test: dummy test: dummy test: dummy test: : : (endless) I just did a search and found some other threads discussing problems with alias: http://ruby-talk.com/34131 http://ruby-talk.com/28977 However, I am pointing out something that is slightly different, since it is not so much an issue of namespace pollution or of keeping the 'prior' version, as it is keeping the 'original' version! 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? Guy N. Hurst