Skip to content
Open
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion lib/super_diff/core/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,13 @@ def object_address_for(object)
def object_address_for(object)
# Sources: <https://bugs.ruby-lang.org/issues/15408> and <https://bugs.ruby-lang.org/issues/15626#Object-ID>
json = JSON.parse(ObjectSpace.dump(object))
json.is_a?(Hash) ? '0x%016x' % Integer(json['address'], 16) : ''
if json.is_a?(Hash) && json['address']
'0x%016x' % Integer(json['address'], 16)
else
''
end
rescue JSON::ParserError
''
end
else
def object_address_for(object)
Expand Down
9 changes: 9 additions & 0 deletions spec/unit/core/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@
expect(helper.object_address_for(1.0)).to eq('')
end

it 'returns an empty string for large Floats whose ObjectSpace.dump is invalid JSON' do
expect(helper.object_address_for(100_000.1)).to eq('')
expect(helper.object_address_for(-100_000.1)).to eq('')
end

it 'returns an empty string for Symbols' do
expect(helper.object_address_for(:sym)).to eq('')
end

it 'returns an object address for Objects' do
object = Object.new
address = JSON.parse(ObjectSpace.dump(object))['address']
Expand Down