Skip to content

Commit

Permalink
Implement ObjectSpace::WeakMap#delete and ObjectSpace::WeakKeyMap#delete
Browse files Browse the repository at this point in the history
[Feature #19561]

It's useful to be able to remove references from weak maps.
  • Loading branch information
byroot authored and eregon committed Apr 25, 2023
1 parent edf9420 commit b19eb32
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
40 changes: 40 additions & 0 deletions core/objectspace/weakkeymap/delete_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require_relative '../../../spec_helper'

ruby_version_is '3.3' do
describe "ObjectSpace::WeakKeyMap#delete" do
it "removes the entry and returns the deleted value" do
m = ObjectSpace::WeakKeyMap.new
key = Object.new
value = Object.new
m[key] = value

m.delete(key).should == value
m.key?(key).should == false
end

it "uses equality semantic" do
m = ObjectSpace::WeakKeyMap.new
key = "foo".upcase
value = Object.new
m[key] = value

m.delete("foo".upcase).should == value
m.key?(key).should == false
end

it "calls supplied block if the key is not found" do
key = Object.new
m = ObjectSpace::WeakKeyMap.new
return_value = m.delete(key) do |yielded_key|
yielded_key.should == key
5
end
return_value.should == 5
end

it "returns nil if the key is not found when no block is given" do
m = ObjectSpace::WeakMap.new
m.delete(Object.new).should == nil
end
end
end
30 changes: 30 additions & 0 deletions core/objectspace/weakmap/delete_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require_relative '../../../spec_helper'

ruby_version_is '3.3' do
describe "ObjectSpace::WeakMap#delete" do
it "removes the entry and returns the deleted value" do
m = ObjectSpace::WeakMap.new
key = Object.new
value = Object.new
m[key] = value

m.delete(key).should == value
m.key?(key).should == false
end

it "calls supplied block if the key is not found" do
key = Object.new
m = ObjectSpace::WeakMap.new
return_value = m.delete(key) do |yielded_key|
yielded_key.should == key
5
end
return_value.should == 5
end

it "returns nil if the key is not found when no block is given" do
m = ObjectSpace::WeakMap.new
m.delete(Object.new).should == nil
end
end
end

0 comments on commit b19eb32

Please sign in to comment.