Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove object_id use in Set#inspect #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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.
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