From: Robert Feldt Date: 2003-09-16T03:14:05+09:00 Subject: Re: performance and style advice requested Dave Brown skrev den Tue, 16 Sep 2003 02:48:04 +0900: > def fact(x) > @facts[x] ||= (1..x).inject(1) { |a,i| a*i } > end Nice one. With the risc of losing readability here is an even more efficient one: @facts = [1] def fact(n) @facts[n] ||= (@facts.last..n).inject(1) {|f, i| @facts[i] = i * f} end since you won't need to recalc more than the ones between largest memoized and your new one. In practice this tweak would probably make very little difference... ;) /Robert