weak_key_map
A backport of ObjectSpace::WeakKeyMap for Ruby < 3.3
Introduction
Ruby 3.3 introduced the handy ObjectSpace::WeakKeyMap class to complement ObjectSpace::WeakMap. The WeakMap class holds weak references to both keys and values, meaning if the value for a particular key is garbage collected, the entire key/value pair will be removed from the map. In contrast, the WeakKeyMap class only removes values if the key is garbage collected.
This gem is a backport of WeakKeyMap for older rubies in the 3.x line.
Usage
WeakKeyMaps work in a very similar fashion to a normal Ruby hash, eg:
map = ObjectSpace::WeakKeyMap.new
key = "key"
map[key] = "value"
map["key"] # => "value"
key = nil
GC.start
map["key"] # => nilRunning Tests
bundle exec rake should do the trick.
License
Licensed under the MIT license. See LICENSE for details.
Authors
- Cameron C. Dutro: http://github.com/camertron