From: Mauricio Fernandez Date: 2006-12-28T01:25:01+09:00 Subject: Re: Using an UnboundMethod instead of an alias to redefine a method On Wed, Dec 27, 2006 at 11:30:05PM +0900, Daniel Berger wrote: > I came across a technique for aliasing methods that I have never seen > before [1] and was just too good to pass up. But first, the setup. [...] > So, now our custom Hash#[]= method is bound to an UnboundMethod that no > one else has access to (see the first link below for a better > explanation). Pretty neat, eh? Is there any downside to this approach? > If not, it seems this technique ought to be filed under 'best > practices', and perhaps even abstracted somehow in the core itself. > > Many thanks to Jay Fields [2], whose blog entry led me back to the > original entry by Martin Traverso. That technique is fairly old (I myself tried to popularize it a few years ago). It's cleaner & generally safer than alias_method, but there are three shortcomings: * the environment captured by the closure is often too heavy (extra care needed to get rid of unwanted references is needed) * a method defined with define_method+block cannot take a block under 1.8 (it's possible in 1.9, though) * method calls are much slower [1] some other examples http://thekode.net/ruby/techniques/CapturingMethods.html -- Mauricio Fernandez - http://eigenclass.org - singular Ruby