From: Daniel Berger Date: 2006-12-30T10:55:08+09:00 Subject: Re: Using an UnboundMethod instead of an alias to redefine a method Mauricio Fernandez wrote: > 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) How can we lighten it, if at all? > * a method defined with define_method+block cannot take a block under 1.8 > (it's possible in 1.9, though) Hm, that is a limitation, but at least the simple cases still work. > * method calls are much slower Yes, I saw some of the benchmarks. Maybe we could memoize the binding somehow? I don't know if that even makes sense. I'm just tossing ideas out there. I guess for now I'll live with making my private aliases...private. :) Regards, Dan