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

Fix float issue with object space dump #146

Merged
Merged
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: 6 additions & 2 deletions lib/super_diff/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ 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>
address = JSON.parse(ObjectSpace.dump(object))["address"]
"0x%016x" % Integer(address, 16)
json = JSON.parse(ObjectSpace.dump(object))
if json.is_a?(Hash)
"0x%016x" % Integer(json["address"], 16)
else
""
end
end
else
def object_address_for(object)
Expand Down
14 changes: 14 additions & 0 deletions spec/unit/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@
end
end
end

describe "object_address_for" do
if SuperDiff::Helpers.ruby_version_matches?(">= 2.7.0")
it "returns an empty string for Floats" do
expect(helper.object_address_for(1.0)).to eq("")
end

it "returns an object address for Objects" do
object = Object.new
address = JSON.parse(ObjectSpace.dump(object))["address"]
expect(helper.object_address_for(object)).to eq("0x%016x" % Integer(address, 16))
end
end
end
end

describe 'as class methods' do
Expand Down