Project

General

Profile

Actions

Bug #20068

closed

Strings equality doesn't imply hash equality

Added by glebpom (Gleb Pomykalov) 11 months ago. Updated 11 months ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 3.2.2 (2023-03-30 revision e51014f9c0) [aarch64-linux]
[ruby-core:115760]

Description

It is well-known that the equality of two objects should lead to the equality of their' hashes (https://ruby-doc.org/3.2.2/Object.html#method-i-hash)

Empty strings, where one encoding is not ASCII-compatible (UTF-16 in the following example), and another is ASCII-compatible don't follow this rule:

irb(main):038:0> l = ""
=> ""
irb(main):039:0> r = l.encode('UTF-16LE')
=> ""
irb(main):040:0> l.hash
=> 1212370190964414646
irb(main):041:0> r.hash
=> 1212370190964414642
irb(main):042:0> l.hash == r.hash
=> false
irb(main):043:0> l.eql?(r)
=> true
irb(main):044:0> l
=> ""
irb(main):045:0> r
=> ""
irb(main):046:0> [l,r].uniq
=> ["", ""]
Actions

Also available in: Atom PDF

Like0
Like0