0.0
The project is in a healthy, maintained state
A backport of ObjectSpace::WeakKeyMap for Ruby < 3.3
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies
 Project Readme

weak_key_map

Unit Tests

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"]  # => nil

Running Tests

bundle exec rake should do the trick.

License

Licensed under the MIT license. See LICENSE for details.

Authors