From: John Carter Date: 2006-01-27T08:08:56+09:00 Subject: Re: Memoization? On Fri, 27 Jan 2006, Dave Howell wrote: >> If everybody in your country had a pink car, what would you have? > > Ha ha. Except that if everybody in my country had a Car.create_car("pink") > car, then the entire country would have exactly one car. Exactly. Which is why it's a lousy example, except in the sense it clearly alerts you to the deficiency of the scheme. Where I used the trick most recently was in parsing .c and .h files for various useful info I needed to process the header files #include'd as well. ie. #include "pink_car.h" Initially my code was just... subresult = ParseFile.new( "pink_car.h") but then I noticed I was reparsing common header files far more often than I needed to. Hence memoization trick. A very simple two line change and off I go again with a good burst of speed. Of course after parsing all the files I need to remember to @@memo = nil to actually release the memory... Why use a class variable for this? It's Good Encapsulation. Who should know whether you have _ever_ produced a pink car before? All the pink car users? Or the car factory? Answer, the Car Factory, ie. the Car class. John Carter Phone : (64)(3) 358 6639 Tait Electronics Fax : (64)(3) 359 4632 PO Box 1645 Christchurch Email : john.carter@tait.co.nz New Zealand Carter's Clarification of Murphy's Law. "Things only ever go right so that they may go more spectacularly wrong later." From this principle, all of life and physics may be deduced.