@@ -380,27 +380,59 @@ def delete_field(name, &block)
380380 end
381381 end
382382
383+ def self . __inspect_supports_identity_set?
384+ true
385+ end
386+
383387 InspectKey = :__inspect_key__ # :nodoc:
384388
385- #
386- # Returns a string containing a detailed summary of the keys and values.
387- #
388- def inspect
389- ids = ( Thread . current [ InspectKey ] ||= [ ] )
389+ # We share the same `ids` value as `Set#inspect`, so we need to use an identity Set
390+ # only we're using a newer version of the `set` gem which is also using an identity Set.
391+ if defined? ( Set . __inspect_supports_identity_set? ) && Set . __inspect_supports_identity_set?
392+
393+ #
394+ # Returns a string containing a detailed summary of the keys and values.
395+ #
396+ def inspect
397+ ids = ( Thread . current [ InspectKey ] ||= Set . new . compare_by_identity )
398+
399+ detail = if ids . add? ( self )
400+ begin
401+ @table . map { |k , v | " #{ k } =#{ v . inspect } " } . join ( ',' )
402+ ensure
403+ ids . remove ( self )
404+ end
405+ else
406+ ' ...'
407+ end
390408
391- detail = if ids . include? ( object_id )
392- ' ...'
393- else
394- ids << object_id
395- begin
396- @table . map { |k , v | " #{ k } =#{ v . inspect } " } . join ( ',' )
397- ensure
398- ids . pop
409+ [ '#<' , self . class! , detail , '>' ] . join
410+ end
411+
412+ else
413+
414+ #
415+ # Returns a string containing a detailed summary of the keys and values.
416+ #
417+ def inspect
418+ ids = ( Thread . current [ InspectKey ] ||= [ ] )
419+
420+ detail = if ids . include? ( object_id )
421+ ' ...'
422+ else
423+ ids << object_id
424+ begin
425+ @table . map { |k , v | " #{ k } =#{ v . inspect } " } . join ( ',' )
426+ ensure
427+ ids . pop
428+ end
399429 end
430+
431+ [ '#<' , self . class! , detail , '>' ] . join
400432 end
401433
402- [ '#<' , self . class! , detail , '>' ] . join
403434 end
435+
404436 alias :to_s :inspect
405437
406438 attr_reader :table # :nodoc:
0 commit comments