Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created November 15, 2025 19:34
Show Gist options
  • Select an option

  • Save havenwood/7c332691c48dd9a2f8dca65c433d74ab to your computer and use it in GitHub Desktop.

Select an option

Save havenwood/7c332691c48dd9a2f8dca65c433d74ab to your computer and use it in GitHub Desktop.
An example of ObjectSpace::WeakKeyMap, which was introduced in Ruby 3.3
require 'delegate'
class Tags < SimpleDelegator
def initialize = super(ObjectSpace::WeakKeyMap.new)
def tagged?(key) = key?(key)
end
TAGS = Tags.new
lamb = -> {}
TAGS[lamb] = :a_tag
TAGS.tagged?(lamb) #=> true
lamb = nil
TAGS.tagged?(lamb) #=> false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment