[ruby-core:113955] [Ruby master Bug#19739] Key cannot be found in a Hash when slice! method is applied to the key
From:
"byroot (Jean Boussier) via ruby-core" <ruby-core@...>
Date:
2023-06-20 14:55:35 UTC
List:
ruby-core #113955
Issue #19739 has been updated by byroot (Jean Boussier).
That is very interesting.
The two strings are identical (aside from one being frozen):
```ruby
require "objspace"
hash =3D {}
key =3D "ABC O=DC"
p key.hash
key.slice!(/ o=FC$/i)=20
p key.hash
puts ObjectSpace.dump(key)
hash[key] =3D true
p "#{key} in #{hash}: value is #{hash[key].inspect}"
puts ObjectSpace.dump(hash.keys.first)
p [hash.keys.first, key, hash.keys.first =3D=3D key]
p [hash.keys.first.hash, key.hash]
```
```
1883393806954673666
-2878200511277543492
{"address":"0x10129f048", "type":"STRING", "class":"0x1011c2418", "embedded=
":true, "bytesize":3, "encoding":"UTF-8", "memsize":40, "flags":{"wb_protec=
ted":true}}
"ABC in {\"ABC\"=3D>true}: value is nil"
{"address":"0x10129ebc0", "type":"STRING", "class":"0x1011c2418", "frozen":=
true, "embedded":true, "fstring":true, "bytesize":3, "value":"ABC", "encodi=
ng":"UTF-8", "memsize":40, "flags":{"wb_protected":true}}
["ABC", "ABC", true]
[-2878200511277543491, -2878200511277543491]
```
The hash code is the same, and `=3D=3D` returns true.
I had a quick look at recent changes in `String#slice!` that could explain =
why it's fixed in 3.2, but didn't spot anything yet.
What is interesting though is that the first access to `key.hash` returns `=
-2878200511277543492`, but then later it consistently returns `-28782005112=
77543491`. I wonder if there is an off by one bug somewhere.
----------------------------------------
Bug #19739: Key cannot be found in a Hash when slice! method is applied to =
the key
https://bugs.ruby-lang.org/issues/19739#change-103610
* Author: ilya.andreyuk (Ilya Andreyuk)
* Status: Open
* Priority: Normal
* ruby -v: 3.1.4
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN
----------------------------------------
Hello,
Key cannot be found in a Hash when slice! method is applied to the key. I c=
annot reproduce this behaviour with sub! or gsub! methods:
``` ruby
hash =3D {}
key =3D "ABC O=DC"
key.slice!(/ o=FC$/i)=20
# key.sub!(/ o=FC$/i, '')
# key.gsub!(/ o=FC$/i, '')
hash[key] =3D true
"#{key} in #{hash}: value is #{hash[key]}"
=3D> "ABC in {\"ABC\"=3D>true}: value is "
```
Also it seems that ruby 2.7.6 and 3.2.2 don't have this issue.
--=20
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-c=
ore.ml.ruby-lang.org/