Skip to content
This repository was archived by the owner on Apr 15, 2026. It is now read-only.

Commit a045f65

Browse files
committed
Remove object_id use in Set#inspect
1 parent efc8c8c commit a045f65

1 file changed

Lines changed: 39 additions & 11 deletions

File tree

lib/set.rb

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -812,23 +812,51 @@ def join(separator=nil)
812812
to_a.join(separator)
813813
end
814814

815+
def self.__inspect_supports_identity_set?
816+
true
817+
end
818+
815819
InspectKey = :__inspect_key__ # :nodoc:
816820

817-
# Returns a string containing a human-readable representation of the
818-
# set ("#<Set: {element1, element2, ...}>").
819-
def inspect
820-
ids = (Thread.current[InspectKey] ||= [])
821+
# We share the same `ids` value as `OpenStruct#inspect`, so we need to use an identity Set
822+
# only we're using a newer version of the `ostruct` gem which is also using an identity Set.
823+
if defined?(OpenStruct.__inspect_supports_identity_set?) && OpenStruct.__inspect_supports_identity_set?
821824

822-
if ids.include?(object_id)
823-
return sprintf('#<%s: {...}>', self.class.name)
825+
# Returns a string containing a human-readable representation of the
826+
# set ("#<Set: {element1, element2, ...}>").
827+
def inspect
828+
ids = (Thread.current[InspectKey] ||= Set.new.compare_by_identity)
829+
830+
unless ids.add?(self)
831+
return sprintf('#<%s: {...}>', self.class.name)
832+
end
833+
834+
begin
835+
return sprintf('#<%s: {%s}>', self.class, to_a.inspect[1..-2])
836+
ensure
837+
ids.remove(self)
838+
end
824839
end
825840

826-
ids << object_id
827-
begin
828-
return sprintf('#<%s: {%s}>', self.class, to_a.inspect[1..-2])
829-
ensure
830-
ids.pop
841+
else
842+
843+
# Returns a string containing a human-readable representation of the
844+
# set ("#<Set: {element1, element2, ...}>").
845+
def inspect
846+
ids = (Thread.current[InspectKey] ||= [])
847+
848+
if ids.include?(object_id)
849+
return sprintf('#<%s: {...}>', self.class.name)
850+
end
851+
852+
ids << object_id
853+
begin
854+
return sprintf('#<%s: {%s}>', self.class, to_a.inspect[1..-2])
855+
ensure
856+
ids.pop
857+
end
831858
end
859+
832860
end
833861

834862
alias to_s inspect

0 commit comments

Comments
 (0)