From: "byroot (Jean Boussier) via ruby-core" Date: 2023-03-30T11:56:56+00:00 Subject: [ruby-core:113051] [Ruby master Feature#19561] ObjectSpace::WeakMap#delete and ObjectSpace::WeakKeyMap#delete Issue #19561 has been reported by byroot (Jean Boussier). ---------------------------------------- Feature #19561: ObjectSpace::WeakMap#delete and ObjectSpace::WeakKeyMap#delete https://bugs.ruby-lang.org/issues/19561 * 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/