ruby-uebungen/Assignment_022/solution/readme.md

286 B

Lösung: Hashes und Blöcke

class Cache
  def initialize(&callback)
    @data = {}
    @callback = callback
  end

  def add(key, value)
    @data[key] = value
  end

  def [](key)
    @data[key] = @callback.call(key)  unless @data.has_key?(key)
    @data[key]
  end
end