Project

General

Profile

Actions

Bug #12897

closed

Hash failed to recognize the existed key and send not found when using Vector as key

Bug #12897: Hash failed to recognize the existed key and send not found when using Vector as key

Added by KiraYao (智偉 姚) almost 10 years ago. Updated almost 10 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.0.0p648 (2015-12-16 revision 53162) [x86_64-linux]
[ruby-core:77906]

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 (智偉 姚) almost 10 years ago Actions #1 [ruby-core:77910]

  • 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) almost 10 years ago Actions #2 [ruby-core:77924]

  • 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) almost 10 years ago Actions #3 [ruby-core:77940]

  • 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) almost 10 years ago Actions #4 [ruby-core:77971]

  • Status changed from Feedback to Rejected

I missed 0.0 and 0.

Actions

Also available in: PDF Atom