Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 39 additions & 11 deletions lib/set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -812,23 +812,51 @@ def join(separator=nil)
to_a.join(separator)
end

def self.__inspect_supports_identity_set?
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true
end

InspectKey = :__inspect_key__ # :nodoc:

# Returns a string containing a human-readable representation of the
# set ("#<Set: {element1, element2, ...}>").
def inspect
ids = (Thread.current[InspectKey] ||= [])
# We share the same `ids` value as `OpenStruct#inspect`, so we need to use an identity Set
# only we're using a newer version of the `ostruct` gem which is also using an identity Set.
Comment on lines +821 to +822
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# We share the same `ids` value as `OpenStruct#inspect`, so we need to use an identity Set
# only we're using a newer version of the `ostruct` gem which is also using an identity Set.
# We share the same thread-local `ids` value as `OpenStruct#inspect`, so we need to use an identity Set
# only when we're using a newer version of the `ostruct` gem which is also using an identity Set.

if defined?(OpenStruct.__inspect_supports_identity_set?) && OpenStruct.__inspect_supports_identity_set?

if ids.include?(object_id)
return sprintf('#<%s: {...}>', self.class.name)
# Returns a string containing a human-readable representation of the
# set ("#<Set: {element1, element2, ...}>").
def inspect
ids = (Thread.current[InspectKey] ||= Set.new.compare_by_identity)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


unless ids.add?(self)
return sprintf('#<%s: {...}>', self.class.name)
end

begin
return sprintf('#<%s: {%s}>', self.class, to_a.inspect[1..-2])
ensure
ids.remove(self)
end
end

ids << object_id
begin
return sprintf('#<%s: {%s}>', self.class, to_a.inspect[1..-2])
ensure
ids.pop
else

# Returns a string containing a human-readable representation of the
# set ("#<Set: {element1, element2, ...}>").
def inspect
ids = (Thread.current[InspectKey] ||= [])

if ids.include?(object_id)
return sprintf('#<%s: {...}>', self.class.name)
end

ids << object_id
begin
return sprintf('#<%s: {%s}>', self.class, to_a.inspect[1..-2])
ensure
ids.pop
end
end

end

alias to_s inspect
Expand Down