2010/12/12 "Martin J. D=FCrst" <duerst / it.aoyama.ac.jp>:
>
> There are already such methods. At least group_by. Your proposal seems to=
 be
> a very special way to convert an enumerable to a hash.

Enumerable#categorize is more general than Enumerable#group_by.

Enumerable#group_by can specify only hash keys.
It cannot specify hash values.

> I think I get the idea of how this is supposed to work. But I'm not sure
> what the actual use cases would be. Can you give some?

I see.

Assume a table as follows.

  ary =3D [
    ["matz", "Yukihiro Matsumoto"],
    ["nobu", "Nobuyoshi Nakada"],
    ["akr", "Tanaka Akira"],
    ["usa", "Usaku NAKAMURA"],
    ["naruse", "NARUSE, Yui"],
    ["ko1", "SASADA Koichi"]
  ]

Enumerable#categorize can be used to generate a hash
from the right elements to left elements.
(and left elements to right elements too.)

  pp ary.categorize {|e| [e[1], e[0]] }
  #=3D>
  {"Yukihiro Matsumoto"=3D>["matz"],
   "Nobuyoshi Nakada"=3D>["nobu"],
   "Tanaka Akira"=3D>["akr"],
   "Usaku NAKAMURA"=3D>["usa"],
   "NARUSE, Yui"=3D>["naruse"],
   "SASADA Koichi"=3D>["ko1"]}

Since Enumerable#group_by cannot specify hash values,
it cannot do it.

> Who will be able to remember exactly what these options are,...? Most oth=
er
> methods, in particular on Enumerables, don't have that many options. I th=
ink
> that's for a good reason.

I think option argument is adopted to ruby increasingly.
--=20
Tanaka Akira