From: Robert Feldt Date: 2003-09-15T23:23:35+09:00 Subject: Re: performance and style advice requested Ben Giddings skrev den Mon, 15 Sep 2003 22:35:14 +0900: >> So my version is: >> >> module Comb >> # memoize results for speed >> FactMemo, CombMemo = [1, 1, 2, 6, 24, 120, 720], {} >> >> # factorial >> def Comb.fact(n) >> return 0 if n < 0 >> FactMemo[n] ||= (n * fact(n-1)) >> end > > I like the fact you wrapped the functionality in a module, but if we're using Ruby idioms, the FactMemo and CombMemo variables should really not be named with leading uppercase letters. They look like classes (or constants) and may be treated as such by Ruby. I'd recommend instead fact_memo and comb_memo. > Yeah, I guess you're right in a way. On the other hand they *are* constants since the array and the hash are the same objects even if their contents change... ;) To me its more a question of what feels right than its about what is more of a Ruby idiom. I could be wrong though... The more logical to me following your thinking would be @fact_memo and @comb_memo. Or to use the memoize extension. :) /Robert