Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
ObjectSpace::WeakKeyMap#delete
map.delete(key) → value or nil
map.delete(key) {|key| ... } → object
weakmap.c
View on GitHub ↗
static VALUE
wkmap_delete(VALUE self, VALUE key)
{
struct weakkeymap *w;
TypedData_Get_Struct(self, struct weakkeymap, &weakkeymap_type, w);
VALUE orig_key = key;
st_data_t orig_key_data = (st_data_t)&orig_key;
st_data_t orig_val_data;
if (st_delete(w->table, &orig_key_data, &orig_val_data)) {
VALUE orig_val = (VALUE)orig_val_data;
rb_gc_remove_weak(self, (VALUE *)orig_key_data);
ruby_sized_xfree((VALUE *)orig_key_data, sizeof(VALUE));
return orig_val;
}
if (rb_block_given_p()) {
return rb_yield(key);
}
else {
return Qnil;
}
}
Deletes the entry for the given key and returns its associated value.
If no block is given and key is found, deletes the entry and returns the associated value:
Example
m = ObjectSpace::WeakKeyMap.new key = "foo" # to hold reference to the key m[key] = 1 m.delete("foo") # => 1 m["foo"] # => nil
If no block given and key is not found, returns nil.
If a block is given and key is found, ignores the block, deletes the entry, and returns the associated value:
Example
m = ObjectSpace::WeakKeyMap.new key = "foo" # to hold reference to the key m[key] = 2 m.delete("foo") { |key| raise 'Will never happen'} # => 2
If a block is given and key is not found, yields the key to the block and returns the block’s return value:
Example
m = ObjectSpace::WeakKeyMap.new m.delete("nosuch") { |key| "Key #{key} not found" } # => "Key nosuch not found"