From: Robert Klemme Date: 2003-09-16T05:08:35+09:00 Subject: Re: performance and style advice requested "Robert Feldt" schrieb im Newsbeitrag news:oprvjzzqcmoglyup@mail1.telia.com... > Robert Feldt skrev den Tue, 16 Sep 2003 03:14:05 +0900: > > > @facts = [1] > > def fact(n) > > @facts[n] ||= (@facts.last..n).inject(1) {|f, i| @facts[i] = i * f} > > end > > > Oops, it should be > > @facts = [1] IMHO rather: @facts = [0,1] > def fact(n) > @facts[n] ||= (@facts.length..n).inject(@facts.last) {|f, i| @facts[i] = i * f} > end And, I think there's a subtle bug with the inject parameter. Another version: def fact(n) @facts ||= [0,1] @facts[n] ||= (@facts.length..n).inject(@facts[-1]) {|f, i| @facts[i] = i * f} end And: Dave, thanks for this inspiring reduction! I haven't grown familiar to inject() yet... Kind regards robert