Skip to content

Commit c716b8e

Browse files
committed
Advent of Changelog: Day 7
1 parent 7ae4182 commit c716b8e

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

_src/3.3.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,40 @@ Allows to assign a string to be rendered as class/module's `#name`, without assi
6464

6565
### `ObjectSpace::WeakKeyMap`
6666

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.
6870
* **Discussion:** [Feature #18498]
6971
* **Documentation:** [ObjectSpace::WeakKeyMap](https://docs.ruby-lang.org/en/master/ObjectSpace/WeakKeyMap.html)
7072
* **Code:**
71-
* **Notes:**
73+
* **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.
7274

7375
### `ObjectSpace::WeakMap#delete`
7476

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.
7678
* **Discussion:** [Feature #19561]
7779
* **Documentation:** [ObjectSpace::WeakMap#delete](https://docs.ruby-lang.org/en/master/ObjectSpace/WeakMap.html#method-i-delete)
7880
* **Code:**
81+
```ruby
82+
files_to_close = ObjectSpace::WeakMap.new
83+
file1 = File.new('README.md')
84+
file2 = File.new('NEWS.md')
85+
86+
files_to_close[file1] = true
87+
files_to_close[file2] = true
88+
89+
files_to_close.delete(file1) #=> true
90+
91+
# Attempt to delete non-existing key:
92+
files_to_close.delete(file1) #=> nil
93+
# An optional block can be provided in case the key doesn't exist:
94+
files_to_close.delete(file1) { puts "Already removed"; 0 }
95+
# Prints "Already removed", returns `0`
96+
97+
# The block wouldn't be called if the deletion was effectful:
98+
files_to_close.delete(file2) { puts "Already removed"; 0 }
99+
# Prints nothing, returns true
100+
```
79101
* **Notes:**
80102

81103
### `Proc#dup` and `#clone` call `#initialize_dup` and `#initialize_clone`

0 commit comments

Comments
 (0)