From: "mame (Yusuke Endoh) via ruby-core" Date: 2023-04-14T01:10:50+00:00 Subject: [ruby-core:113231] [Ruby master Feature#19561] ObjectSpace::WeakMap#delete and ObjectSpace::WeakKeyMap#delete Issue #19561 has been updated by mame (Yusuke Endoh). Discussed at the dev meeting. @matz accepted this. ---------------------------------------- Feature #19561: ObjectSpace::WeakMap#delete and ObjectSpace::WeakKeyMap#delete https://bugs.ruby-lang.org/issues/19561#change-102778 * Author: byroot (Jean Boussier) * Status: Open * Priority: Normal ---------------------------------------- I just realized in https://bugs.ruby-lang.org/issues/19560#Ruby-shim that there's no way to remove elements from either WeakMaps. I think they could both benefit from a `delete` method. ### Use case For example, to keep track of a subset of instances of a class, you may need to remove some from the list when a condition changes: ```ruby class Connection CLOSE_ON_FORK = ObjectSpace::WeakMap.new class << self def after_fork CLOSE_ON_FORK.each_key do |connection| connection.close if connection.close_on_fork? end end end def close_on_fork=(enabled) if enabled CLOSE_ON_FORK[self] = true else CLOSE_ON_FORK.delete(self) end @close_on_fork = enabled end def close_on_fork? @close_on_fork end end module CloseOnFork IOS = ObjectSpace::WeakMap.new def _fork pid = super if pid == 0 # child ::Connection.after_fork end pid end end Process.singleton_class.prepend(CloseIOOnFork) ``` -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/