From: Mathieu Bouchard Date: 2001-11-22T16:15:02+09:00 Subject: [ruby-talk:26119] Re: call_myself On Thu, 22 Nov 2001, Tobias Reif wrote: > --- the example we all know--- > def fact(n) > if n == 0 > 1 > else > n * fact(n-1) > end > end > ------------------- > Is there something that makes it clear that the method calls itself? It's better for a method not to explicitly call itself, because that way you can insert a method between that method and itself. e.g.: $fact_cache = [] alias orig_fact def fact(n) $fact_cache[n] ||= orig_fact(n) end which is not a speed improvement on the first time, but it is on subsequent times. For functions like fibonacci (the other classic example) it's always a tremendous speed improvement (exponential vs linear) Robert has written a package (memoize) for that. iirc it's much like what Mark Jason Dominus has done on the Perl side. There are other interesting things you can do by "inserting" a method like that (e.g. printing the function call tree) ________________________________________________________________ Mathieu Bouchard http://hostname.2y.net/~matju