From: Boris Schmid Date: 2008-05-29T03:28:58+09:00 Subject: subclass of hash, that times last read Hi all. I'm rather unfamiliar with the subclass / superclass principle, but I just tried to write one. The result is below here, and should be a hash-like class, with the exception that it stores the last-accessed time (the purpose of this being that I can more easily delete entries from the hash that haven't been used in a while - I'm using a hash for caching a costly function, but over time the hash grows big and slow, with a lot of old entries in it.. I'm trying out different ways of pruning the hash, and this seemed to be an interesting option). The below code seems to work fine, but it would be a comfort to me to know if I'm introducing several hairy bug-prone things if I add this to my code, or that this is the relatively safe and correct way to do it. class Myhash < Hash alias :old_write :[]= def []=(key,value) old_write(key,[value,Time.now]) end alias :old_read :[] def [](key) value = old_read(key) old_write(key,[value[0],Time.now]) if value.respond_to?(:[]) old_read(key) end end hash = Myhash.new 1000.times {hash[rand] = rand} sleep 7 1000.times {hash[rand] = rand} hash.delete_if {|k,v| Time.now - v[1] > 5} Which should delete the 1000 entries from the hash that were created in the first loop. -- Posted via http://www.ruby-forum.com/.