From: Robert Klemme Date: 2008-02-20T19:15:27+09:00 Subject: Re: Object#freeze as a basis for caching of method results? 2008/2/19, Shot (Piotr Szotkowski) : > After profiling my code I figured out I might want to attempt some > caching of results obtained in 'heavy' methods. Is there an idiomatic > way to do method-results caching in Ruby? http://raa.ruby-lang.org/project/memoize/ > I guess my question boils down to what does exactly Object#freeze > prevent from being modified – simply all the properties? Yes. You cannot assign instance variables any more once an instance is frozen. > If so, does it mean I can only have one method like the above (because > if some other method did any freezing, I couldn't initialise the @seps > cache when the first call to separations occurs)? An alternative approach would be to use current state as cache key, i.e. create an immutable copy and stuff that along with calculation results into a Hash. > Is there any 'best practice' in Ruby > with regards to caching method results? Memoize, see above. > Is there any obvious other approach to such caching? (I thought about > having a @cache instance variable that would get reset on object > changes, but I'm not sure how to obtain all the list of methods that > can change an object – i.e., methods that throw error when a given > object is frozen.) I suggest you do not inherit Set. In that case it's easy: *you* define which methods change the state of your class. Kind regards robert -- use.inject do |as, often| as.you_can - without end