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

Equality issues #247

Open
mauricioszabo opened this issue May 15, 2019 · 4 comments
Open

Equality issues #247

mauricioszabo opened this issue May 15, 2019 · 4 comments

Comments

@mauricioszabo
Copy link

I was working on a project, then I've found a case when a == b is true but b == a is false.

Minimum code that I could find the error:

require 'hamster'
require 'bigdecimal'

a = Hamster.from({
  items: [{
    amount: 0.0,
    quantity: 1
  }]
})

b = Hamster.from({
  items: [{
    amount: BigDecimal('0.0'),
    quantity: 1
  }]
})

a == b
# => false
b == a
# => true
@alexdowad
Copy link
Contributor

Simpler repro uses a = Hamster.from({amount: 0.0}); b = Hamster.from({amount: BigDecimal('0.0')}).

@alexdowad
Copy link
Contributor

This is inherited from the idiosyncratic behavior of Hash#eql?. (That's ::Hash, not Hamster::Hash!)

{amount: 0.0}.eql?({amount: BigDecimal('0.0')})
# => false
{amount: BigDecimal('0.0')}.eql?({amount: 0.0})
# => true

Questions:

  1. Is that the intended behavior of #eql? for Ruby's built-in Hash?
  2. Should Hamster::Hash#== actually be using #eql?? It seems that chaining to #== would be more appropriate.

@alexdowad
Copy link
Contributor

From ri BasicObject#==:

"Numeric types... perform type conversion across ==, but not across eql?..."

It looks like implementing Hamster::Hash#== using ::Hash#eql? is wrong. I'll just look at the git commit history to see if there was a reason why that was done.

@alexdowad
Copy link
Contributor

Looks like I wrote that code back in 2014! Interesting to see that it has stood up for 5 years, only for a lurking bug to show up now...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants