From: Trans Date: 2006-09-09T07:06:26+09:00 Subject: Re: better alias_method Ken Bloom wrote: > On Sat, 09 Sep 2006 02:54:35 +0900, Trans wrote: > > > ara.t.howard@noaa.gov wrote: > >> heh. i think you may be hitting the same thing i was. here's some food for > >> thought: > >> > >> > >> this works: > >> > >> harp:~ > cat a.rb > >> class C > >> include Module.new{ def m() 'a' end } > >> include Module.new{ def m() super + 'b' end } > >> include Module.new{ def m() super + 'c' end } > >> end > >> > >> p C.new.m > >> > >> > >> harp:~ > ruby a.rb > >> "abc" > >> > >> > >> while this does not: > >> > >> harp:~ > cat a.rb > >> class C > >> def m() '' end > >> > >> include Module.new{ def m() super + 'a' end } > >> include Module.new{ def m() super + 'b' end } > >> include Module.new{ def m() super + 'c' end } > >> end > >> > >> p C.new.m > >> > >> > >> harp:~ > ruby a.rb > >> "" > >> > >> even more distilled: > >> > >> harp:~ > cat a.rb > >> class C > >> def m() '' end > >> > >> include Module.new{ def m() 'a' end } > >> end > >> > >> p C.new.m > >> > >> > >> harp:~ > ruby a.rb > >> "" > >> > >> > >> i find that behviour quite suprising... in fact, it seems like a bug, but i'm > >> probably wrong... thoughts? > > > > Not at all becasue inclusion adds to the inheritance chain, it doesn't > > inject methods. You can of course define an #inject method if you like. > > But it's still difficult to do the orginial intent of this thread b/c > > of how #super works. > > > > Personally I think it would cool if you could redefine a method calling > > on the previous definition just as one can redefine a variable calling > > on it's previous definition. Eg. > > > > a = 1 > > a = a + 1 > > > > def a ; 1 ; end > > def a ; a + 1 ; end > > > > This would eliminate the need for #super except in cases of recursion, > > in which case a special notation is also needed for the definition. Eg. > > def_rec a, or something. But that's cool b/c it would be safer. > > > > In anycase, alternate solutions to the alias issue include > > #wrap_method, > > see http://www.erikveen.dds.nl/monitorfunctions/index.html#implementation > > > #alias_chain, Matz' :before and :after system (semi-aop) and cuts (aop). > > Where is there information about these? All of these can be found in Facets (facets.rubyforge.org) except Matz' system which is just a concept given in his keynotes. (see http://www.rubyist.net/~matz/slides/rc2005/mgp00006.html). Note #alias_chain is actually #alias_method_chain and is borrowed from Rails. It's quite clever, but has limited usage. T.