Bug #12897
closedHash failed to recognize the existed key and send not found when using Vector as key
Description
When I was using Vector as the key to the Hash, Hash sometimes failed to recognize the key and send nil, while the key-value pair actually existed.
Example as followed:
irb(main):002:0> require"matrix"
=> true
irb(main):003:0> Vector[0.1E1, 0.2E1, 0.0] == Vector[0.1E1, 0.2E1, 0]
=> true
irb(main):004:0> p = { Vector[0.1E1, 0.2E1, 0.0] => 1}
=> {Vector[1.0, 2.0, 0.0]=>1}
irb(main):005:0> p.has_key?(Vector[0.1E1, 0.2E1, 0])
=> false
irb(main):006:0> Vector[0.1E1, 0.2E1, 0] == Vector[0.1E1, 0.2E1, 0.0]
=> true
As you can see, the key is corrected recognized, but the Hash could not recognize it.
Updated by KiraYao (智偉 姚) over 7 years ago
- ruby -v changed from ruby 2.0.0p648 (2015-12-16 revision 53162) [x86_64-linux] to ruby 2.0.0p648 (2015-12-16 revision 53162) [x86_64-linux]
also affected version ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux].
Updated by marcandre (Marc-Andre Lafortune) over 7 years ago
- Status changed from Open to Rejected
This is not a bug. Hash#[]
uses eql?
and not ==
to match keys.
0 == 0.0 # => true
0.eql? 0.0 # => false
{0 => 42}[0.0] # => nil
{0.0 => 42}[0.0] # => 42
Updated by nobu (Nobuyoshi Nakada) over 7 years ago
- Status changed from Rejected to Feedback
It should work.
$ ruby2.0 -v -rmatrix -e 'p = { Vector[1.0, 2.0, 0.0] => 1}' -e 'p p.key?(Vector[1.0, 2.0, 0.0])'
ruby 2.0.0p648 (2015-12-16 revision 53162) [universal.x86_64-darwin15.3.0]
true
Is your matrix library the standard library?
Updated by nobu (Nobuyoshi Nakada) over 7 years ago
- Status changed from Feedback to Rejected
I missed 0.0
and 0
.