From: Dave Thomas Date: 2001-05-01T03:55:51+09:00 Subject: [ruby-talk:14449] Re: How to do it the Ruby-way 1 Stefan Matthias Aust writes: > Hi! > > I'm still a Ruby newbie and wonder what is the best way to express the > following algorithm in Ruby: A cache shall provide a service to access > the contents of files so that files are read only once. > > class Cache > @@cache = {} > def Cache.get(name) > cache.fetch(name) {@@cache[name] = File.open(name).read} > end > end A useful idiom is @@cache[name] ||= File.open(name).read Regards Dave