From: "nicholasmabry@..." Date: 2008-06-11T11:49:46+09:00 Subject: Re: storing values for later use Hey Tom, The previous responders were right on. Memoization is the concept of caching the results of a function call. Googling 'ruby memoization' will yield far better descriptions than mine, but here's a simple example using the ruby gem Memoize. When it is mixed-in to a class definition, it adds the method memoize() to instances of the class. When you want to begin storing results of a calculation, call memoize on the object with the symbol name (the colon string) of the method you'd like to begin caching. require 'memoize' class CalcLibrary include Memoize def call_c(function_name, *args_array) # This method calls into the C extension (I assume) # And returns an array (we'll just return randoms) return_array = [] srand Time.now.usec return_array << rand(10000) return_array << rand(10000) end end # A quick test puts "-- No memoize() call yet --" clib = CalcLibrary.new puts "Result one:" puts clib.call_c("rand")[1] puts "Result two:" puts clib.call_c("rand")[1] puts "Result three:" puts clib.call_c("rand", 2)[1] puts "-- Memoized --" clib.memoize :call_c puts "Result one:" puts clib.call_c("rand")[1] puts "Result two:" # Should be the same as result 1 if cached puts clib.call_c("rand")[1] puts "Result three:" puts clib.call_c("rand", 2)[1] The Memoize module also includes the ability to store and load caches out to a file, but it's easy to get into trouble that way without diving into how it's working under-the-hood. Good luck with your project!