From: Stefan Matthias Aust Date: 2001-05-01T03:40:13+09:00 Subject: [ruby-talk:14447] How to do it the Ruby-way 1 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 Or more abstract, I want to either return an existing value or store and return some new value if it's not already in a Hash. In Smalltalk, I'd have used #at:ifAbsentPut:. With the above Ruby code, I dislike the []= inside the block. Have I overlooked some Hash method? BTW, what's the "official" way to test whether a certain key exists in a Hash object? That class provides has_key?, include?, key? and member?. Why are there four different methods? bye -- Stefan Matthias Aust \/ Truth Until Paradox