Sha256: 5b89fb582bb9c6f7ad94ec4dcc85daafd90f6207c10adc20aa0a0fd0ddc0aa5b
Contents?: true
Size: 509 Bytes
Versions: 31
Compression:
Stored size: 509 Bytes
Contents
# A very simple cache. Works just like a Hash, but can optionally store values # during the fetch process. # # Example: # hc = HashCache.new # hc.fetch("foo") # => nil # hc.fetch("foo"){:bar} # Block gets called because 'foo' is nil # => :bar # hc.fetch("foo"){raise "Block won't be called because 'foo' is cached"} # => :bar class HashCache < Hash def fetch(key, &block) if has_key?(key) self[key] elsif block self[key] = block.call else nil end end end
Version data entries
31 entries across 31 versions & 2 rubygems