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

BigDecimal#hash doesn't return always the same result for given value #1656

Closed
deepj opened this issue Apr 15, 2019 · 4 comments
Closed

BigDecimal#hash doesn't return always the same result for given value #1656

deepj opened this issue Apr 15, 2019 · 4 comments
Assignees

Comments

@deepj
Copy link

deepj commented Apr 15, 2019

This causes a problem i.e. while matching in RSpec change method

TruffleRuby 1.0.0 RC15 (wrong)

BigDecimal('1.2920').hash
# => 716996660
BigDecimal('1.2920').hash
# => 1195473402

MRI 2.6.2 (correct)

BigDecimal('1.2920').hash
# => 615576019346831331
BigDecimal('1.2920').hash
# => 615576019346831331
@deepj
Copy link
Author

deepj commented Apr 15, 2019

This is a real case in my specs when I'm testing if an import of exchange rates is idempotent. Due the above issue it fails though.

expect { service.call }.not_to change { DB[:exchange_rates].select(:rate).all }

@deepj
Copy link
Author

deepj commented May 8, 2019

@nirvdrum Is this an easy fix?

@nirvdrum
Copy link
Collaborator

@deepj Sorry for just getting to this. You pinged me while I was traveling and I just missed it. I'm working on a fix now. It's pretty straightforward. The problem is we're currently hashing BigDecimal objects as if they're arbitrary object references. We need to treat them like value types.

@nirvdrum
Copy link
Collaborator

This issue has been fixed as of 3b887a1. We didn't customize the hash method for BigDecimal, so it was falling back to the default, which just tries to assign an identifier to each object with good hash distribution. The fix should appear in our next release.

@nirvdrum nirvdrum added this to the 20.0-beta1 milestone May 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants