1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Allow non-finalizable objects in ObjectSpace::WeakMap

[feature #16035]

This goes one step farther than what nobu did in [feature #13498]

With this patch, special objects such as static symbols, integers, etc can be used as either key or values inside WeakMap. They simply don't have a finalizer defined on them.

This is useful if you need to deduplicate value objects
This commit is contained in:
Jean Boussier 2019-08-01 14:41:21 -04:00 committed by Nobuyoshi Nakada
parent e4be2fda3d
commit a4a19b114b
Notes: git 2019-08-31 04:40:14 +09:00
2 changed files with 30 additions and 21 deletions

View file

@ -16,16 +16,27 @@ class TestWeakMap < Test::Unit::TestCase
def test_aset_const
x = Object.new
assert_raise(ArgumentError) {@wm[true] = x}
assert_raise(ArgumentError) {@wm[false] = x}
assert_raise(ArgumentError) {@wm[nil] = x}
assert_raise(ArgumentError) {@wm[42] = x}
assert_raise(ArgumentError) {@wm[:foo] = x}
assert_raise(ArgumentError) {@wm[x] = true}
assert_raise(ArgumentError) {@wm[x] = false}
assert_raise(ArgumentError) {@wm[x] = nil}
assert_raise(ArgumentError) {@wm[x] = 42}
assert_raise(ArgumentError) {@wm[x] = :foo}
@wm[true] = x
assert_same(x, @wm[true])
@wm[false] = x
assert_same(x, @wm[false])
@wm[nil] = x
assert_same(x, @wm[nil])
@wm[42] = x
assert_same(x, @wm[42])
@wm[:foo] = x
assert_same(x, @wm[:foo])
@wm[x] = true
assert_same(true, @wm[x])
@wm[x] = false
assert_same(false, @wm[x])
@wm[x] = nil
assert_same(nil, @wm[x])
@wm[x] = 42
assert_same(42, @wm[x])
@wm[x] = :foo
assert_same(:foo, @wm[x])
end
def assert_weak_include(m, k, n = 100)