ruby-uebungen/Assignment_022/solution/readme.md

20 lines
286 B
Markdown
Raw Normal View History

2023-05-25 17:47:19 +02:00
# Lösung: Hashes und Blöcke
2023-05-23 09:19:31 +02:00
2023-05-25 17:49:42 +02:00
```ruby
2023-05-25 17:47:19 +02:00
class Cache
def initialize(&callback)
@data = {}
@callback = callback
end
def add(key, value)
@data[key] = value
end
2023-05-23 09:19:31 +02:00
2023-05-25 17:47:19 +02:00
def [](key)
@data[key] = @callback.call(key) unless @data.has_key?(key)
@data[key]
end
end
```