Module: WeakSet::WeakKeysWithDelete

Defined in:
lib/weak_set/weak_keys_with_delete.rb

Overview

This WeakSet strategy targets Ruby >= 3.3.0. Older Ruby versions require additional indirections implemented in WeakKeys:

Ruby's ObjectSpace::WeakMap uses weak keys and weak values so that either the key or the value can be independently garbage collected. If either of them vanishes, the entry is removed.

The WeakMap also allows to delete entries. This allows us to directly use the WeakMap as a storage the same way a Set uses a Hash object object as storage.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.usable?Bool

Checks if this strategy is usable for the current Ruby version.

Returns:

  • (Bool)

    truethy for Ruby (aka. MRI, aka. YARV) >= 3.3.0, falsey otherwise



28
29
30
31
# File 'lib/weak_set/weak_keys_with_delete.rb', line 28

def self.usable?
  RUBY_ENGINE == "ruby" &&
    ObjectSpace::WeakMap.instance_methods.include?(:delete)
end

Instance Method Details

#add(obj) ⇒ self

Adds the given object to the weak set and return self. Use WeakSet#merge to add many elements at once.

In contrast to other "regular" objects, we will not retain a strong reference to the added object. Unless some other live objects still references the object, it will eventually be garbage-collected.

Examples:

WeakSet[1, 2].add(3)                #=> #<WeakSet: {1, 2, 3}>
WeakSet[1, 2].add([3, 4])           #=> #<WeakSet: {1, 2, [3, 4]}>
WeakSet[1, 2].add(2)                #=> #<WeakSet: {1, 2}>

Parameters:

  • obj (Object)

    an object

Returns:

  • (self)


40
41
42
43
# File 'lib/weak_set/weak_keys_with_delete.rb', line 40

def add(obj)
  @map[obj] = true
  self
end

#clearself

Removes all elements and returns self

Returns:

  • (self)


46
47
48
49
# File 'lib/weak_set/weak_keys_with_delete.rb', line 46

def clear
  @map = ObjectSpace::WeakMap.new
  self
end

#delete?(obj) ⇒ self?

Note:

WeakSet does not test member equality with == or eql?. Instead, it always checks strict object equality, so that, e.g., different strings are not considered equal, even if they may contain the same string content.

Deletes the given object from self and returns self if it was present in the set. If the object was not in the set, returns nil.

Parameters:

  • obj (Object)

Returns:

  • (self, nil)

    self if the given object was deleted from the set or nil if the object was not part of the set



52
53
54
55
56
# File 'lib/weak_set/weak_keys_with_delete.rb', line 52

def delete?(obj)
  # WeakMap#delete returns the vlaue if it was removed. As we set it to
  # true, WeakMap#delete returns either true or nil here.
  self if @map.delete(obj)
end

#each {|element| ... } ⇒ self, Enumerator

Calls the given block once for each live element in self, passing that element as a parameter. Returns the weak set itself.

If no block is given, an Enumerator is returned instead.

Yields:

  • (element)

    calls the given block once for each element in self

Yield Parameters:

  • element (Object)

    the yielded value

Returns:

  • (self, Enumerator)

    self if a block was given or an Enumerator if no block was given.



59
60
61
62
63
64
# File 'lib/weak_set/weak_keys_with_delete.rb', line 59

def each(&block)
  return enum_for(__method__) { size } unless block_given?

  @map.keys.each(&block)
  self
end

#include?(obj) ⇒ Bool

Note:

WeakSet does not test member equality with == or eql?. Instead, it always checks strict object equality, so that, e.g., different strings are not considered equal, even if they may contain the same string content.

Returns true if the given object is included in self, false otherwise.

Parameters:

  • obj (Object)

    an object

Returns:

  • (Bool)

    true if the given object is included in self, false otherwise



67
68
69
# File 'lib/weak_set/weak_keys_with_delete.rb', line 67

def include?(obj)
  @map.key?(obj)
end

#initializevoid

Initialize the weak map



35
36
37
# File 'lib/weak_set/weak_keys_with_delete.rb', line 35

def initialize
  @map = ObjectSpace::WeakMap.new
end

#sizeInteger

Returns the number of live elements in self.

Returns:

  • (Integer)

    the number of live elements in self



72
73
74
# File 'lib/weak_set/weak_keys_with_delete.rb', line 72

def size
  @map.size
end

#to_aArray

Note:

The order of elements on the returned Array is non-deterministic. We do not preserve preserve insertion order.

Returns the live elements contained in self as an Array.

Returns:

  • (Array)

    the live elements contained in self as an Array



77
78
79
# File 'lib/weak_set/weak_keys_with_delete.rb', line 77

def to_a
  @map.keys
end