Skip to content

Commit d0ccdf2

Browse files
authored
Fix float issue with object space dump (#146)
I get `NoMethodError` when doing a diff against a float constant because of how `ObjectSpace.dump(object)` treats floats. ``` # here is the problematic line in helpers.rb address = JSON.parse(ObjectSpace.dump(object))["address"] ObjectSpace.dump(1.0) => "1.00000" ``` This fixes that.
1 parent 1ac51ea commit d0ccdf2

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

lib/super_diff/helpers.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,12 @@ def object_address_for(object)
5252

5353
def object_address_for(object)
5454
# Sources: <https://bugs.ruby-lang.org/issues/15408> and <https://bugs.ruby-lang.org/issues/15626#Object-ID>
55-
address = JSON.parse(ObjectSpace.dump(object))["address"]
56-
"0x%016x" % Integer(address, 16)
55+
json = JSON.parse(ObjectSpace.dump(object))
56+
if json.is_a?(Hash)
57+
"0x%016x" % Integer(json["address"], 16)
58+
else
59+
""
60+
end
5761
end
5862
else
5963
def object_address_for(object)

spec/unit/helpers_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@
3939
end
4040
end
4141
end
42+
43+
describe "object_address_for" do
44+
if SuperDiff::Helpers.ruby_version_matches?(">= 2.7.0")
45+
it "returns an empty string for Floats" do
46+
expect(helper.object_address_for(1.0)).to eq("")
47+
end
48+
49+
it "returns an object address for Objects" do
50+
object = Object.new
51+
address = JSON.parse(ObjectSpace.dump(object))["address"]
52+
expect(helper.object_address_for(object)).to eq("0x%016x" % Integer(address, 16))
53+
end
54+
end
55+
end
4256
end
4357

4458
describe 'as class methods' do

0 commit comments

Comments
 (0)