From: Andrew Stewart Date: 2007-01-05T20:12:08+09:00 Subject: Re: How can I do this better? On 5 Jan 2007, at 09:05, Sam Kong wrote: > I find myself to use the following idiom frequently. > > def foo i > return @cache[i] if @cache[i] > @cache[i] = some_method_that_takes_long_time(i) > end You could also use the 'overlooked feature of Ruby hashes' and put your lengthy method in the block you pass to Hash.new: http://moonbase.rydia.net/mental/blog/programming/overlooked-feature- of-ruby-hashes.html Regards, Andy Stewart