Improve compatibility for Symbol#name

This commit is contained in:
Marc-André Lafortune 2021-12-28 10:41:22 -05:00
parent f03df3b689
commit 2d342b2362
9 changed files with 23 additions and 7 deletions

View File

@ -162,7 +162,6 @@ IGNORE_IN_200 = %w[
]
IGNORE = %w[
3.0.0/symbol/name
3.0.0/ractor/ractor
3.1.0/enumerable/compact
3.1.0/struct/keyword_init

View File

@ -1,11 +1,21 @@
unless Symbol.method_defined? :name
def Backports.symbol_names
@symbol_names ||= ObjectSpace::WeakMap.new
end
if ((ObjectSpace::WeakMap.new[:test] = :test) rescue false)
# WeakMaps accept symbols only in Ruby 2.7+
def Backports.symbol_names
@symbol_names ||= ObjectSpace::WeakMap.new
end
class Symbol
def name
Backports.symbol_names[self] ||= to_s.freeze
class Symbol
def name
Backports.symbol_names[self] ||= to_s.freeze
end
end
else
# For earlier Rubies, we can't pool their strings
class Symbol
def name
to_s.freeze
end
end
end
end

View File

@ -0,0 +1 @@
fails:Symbol#name returns same string instance

View File

@ -0,0 +1 @@
fails:Symbol#name returns same string instance

View File

@ -0,0 +1 @@
fails:Symbol#name returns same string instance

View File

@ -0,0 +1 @@
fails:Symbol#name returns same string instance

View File

@ -0,0 +1 @@
fails:Symbol#name returns same string instance

View File

@ -0,0 +1 @@
fails:Symbol#name returns same string instance

View File

@ -0,0 +1 @@
fails:Symbol#name returns same string instance