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 OpenStruct#inspect #58

Open
wants to merge 2 commits 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
64 changes: 48 additions & 16 deletions lib/ostruct.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -380,27 +380,59 @@ def delete_field(name, &block)
end
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 detailed summary of the keys and values.
#
def inspect
ids = (Thread.current[InspectKey] ||= [])
if ids.include?(object_id)
detail = ' ...'
else
ids << object_id
begin
detail = @table.map do |key, value|
" #{key}=#{value.inspect}"
end.join(',')
ensure
ids.pop
# We share the same `ids` value as `Set#inspect`, so we need to use an identity Set
# only we're using a newer version of the `set` gem which is also using an identity Set.
if defined?(Set.__inspect_supports_identity_set?) && Set.__inspect_supports_identity_set?

#
# Returns a string containing a detailed summary of the keys and values.
#
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.


detail = if ids.add?(self)
begin
@table.map { |k, v| " #{k}=#{v.inspect}" }.join(',')
ensure
ids.remove(self)
end
else
' ...'
end

['#<', self.class!, detail, '>'].join
end
['#<', self.class!, detail, '>'].join

else

#
# Returns a string containing a detailed summary of the keys and values.
#
def inspect
ids = (Thread.current[InspectKey] ||= [])

detail = if ids.include?(object_id)
' ...'
else
ids << object_id
begin
@table.map { |k, v| " #{k}=#{v.inspect}" }.join(',')
ensure
ids.pop
end
end

['#<', self.class!, detail, '>'].join
end

end

alias :to_s :inspect

attr_reader :table # :nodoc:
Expand Down