ruby-uebungen/Assignment_024/solution/readme.md

19 lines
648 B
Markdown
Raw Normal View History

2023-05-23 09:19:31 +02:00
# Lösung: Hashes und Blöcke
<div style="border: 1px solid grey;"><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br></div>```ruby
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
```