Skip to content

Commit 099bd87

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

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

lib/set.rb

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -814,21 +814,45 @@ def join(separator=nil)
814814

815815
InspectKey = :__inspect_key__ # :nodoc:
816816

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

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

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

834858
alias to_s inspect

0 commit comments

Comments
 (0)