You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _src/3.3.md
+25-3Lines changed: 25 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -64,18 +64,40 @@ Allows to assign a string to be rendered as class/module's `#name`, without assi
64
64
65
65
### `ObjectSpace::WeakKeyMap`
66
66
67
-
***Reason:**
67
+
A new "weak map" concept implementation. Unlike `ObjectSpace::WeakMap`, it compares keys by equality (`WeakMap` compares by identity), and only references to keys are weak (garbage-collectible).
68
+
69
+
***Reason:** The idea of a new class grew out of increased usage of `ObjectSpace::WeakMap` (which was once considered internal). In many other languages, concept of "weak map" implies only key references are weak: this allows to use it as a generic "holder of some additional information related to a set of objects while they are alive," or just a weak set of objects (using them as keys and `true` as values): caches, deduplication sets, etc.
***Notes:** The class interface is significantly leaner than `WeakMap`'s, and doesn't provide any kind of iteration methods (which is very hard to implement and use correctly with weakly-referenced objects), so the new class is more like a black box with associations than a collection.
72
74
73
75
### `ObjectSpace::WeakMap#delete`
74
76
75
-
***Reason:**
77
+
***Reason:**`WeakMap` is frequently used to have a loose list of objects that will need some processing at some point of program execution if they are still alive/used (that's why `WeekMap` and not `Array`/`Hash` is chosen in those cases). But it is possible that the code author wants to process objects conditionally, and to remove those which don't need processing anymore—even if they are still alive. `WeekMap` quacks like kind-of simple `Hash`, yet previously provided no way to delete keys.
0 commit comments