Actions
Bug #16902
closedEnumerable#group_by invalid key object
Bug #16902:
Enumerable#group_by invalid key object
Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux]
Description
I actually want to group by a customized string object and it seems to be ignored. I've tested this with ruby 2.5 and 2.7 and the behavior seems to be the same. Not really sure if this is expected, but the documentation means it's grouped by the block result.
[15] pry(main)> Moo = Module.new{def moo; "hui"; end}
=> Moo
[16] pry(main)> [+'moo'.extend(Moo), +'moo'.extend(Moo)].group_by{ _1 }['moo'].map(&:moo)
=> ["hui", "hui"]
[17] pry(main)> [+'moo'.extend(Moo), +'moo'.extend(Moo)].group_by{ _1 }.keys.map(&:moo)
NoMethodError: undefined method `moo' for "moo":String
from (pry):17:in `map'
Updated by nobu (Nobuyoshi Nakada) over 5 years ago
- Status changed from Open to Rejected
16 and 17 call moo method on different objects.
Hashs store frozen copies of String keys, otherwise if a key gets modified then the element associated to the modified key can no longer be accessed, until rehashed.
As this "copy" is not made with clone method, extended Modules are not copied.
Actions