Project

General

Profile

Actions

Bug #16902

closed

Enumerable#group_by invalid key object

Bug #16902: Enumerable#group_by invalid key object

Added by tonobo (Tim Foerster) over 5 years ago. Updated over 5 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux]
[ruby-core:98450]

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 Actions #1 [ruby-core:98451]

  • 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

Also available in: PDF Atom