From: Robert Klemme Date: 2003-09-16T17:30:28+09:00 Subject: Re: performance and style advice requested "Robert Feldt" schrieb im Newsbeitrag news:oprvj614c2oglyup@mail1.telia.com... > Robert Klemme skrev den Tue, 16 Sep 2003 05:08:35 +0900: > > >> 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 > > > Can you spell it out for me? I fail to see the difference. Argh! *My* fault! I read "@facts.last" as "last index", but it's "last element". Oh just forget it... /me scuffles away into some corner and coils up. > And I wouldn't do the initial assignment inside the method when > we're discussing performance... Yeah. I just put it there to ensure proper operation if someone copied and pasted the code somewhere else. Regards robert