From: Thomas Hafner Date: 2009-05-02T22:40:04+09:00 Subject: lazy lookup Hello, how about the following implementation for lazy lookups? def define_lookup h = lambda{ |k| h = {} # ... fill h (expensive!) ... h[k] } self.class.class_eval{ define_method(:lookup){ |k| h[k] } } end Once ``define_lookup'' has been called, the method ``lookup'' is defined and will act in a lazy manner. Just call ``lookup'' as often as you need, but only for the first time the expensive operation (e.g. dealing with files) will be performed. Due to duck typing no condition needs to be checked: for the first call ``h[k]'' implies calling the lambda, for the next times ``h[k]'' implies accessing the hash. (Tried with Ruby 1.8.6.) Regards Thomas