On Oct 8, 8:00=A0pm, Pete Hodgson <phodg... / lyris.com> wrote:
> Hi folks,
>
> Given:
>
> Person =3D Struct.new(:name,:age,:city)
> people_array =3D [
> =A0 =A0 Person.new('bob',12,'SFO'),
> =A0 =A0 Person.new('dave',14,'NYC'),
> =A0 =A0 Person.new('jane',6,'LDN') ]
>
> people_map =3D {}
> people_array.each{ |p| people_map[p.name] =3D p }
>
> Is there a cleaner way to build people_map? I'm thinking there might be
> something like:
>
> people_map =3D people_array.to_map{ |x| x.name }
>
> in the standard library somewhere that I don't know of.
>
> Thanks for any pointers,
> Pete

Hash[ * people_array.map{|x| [ x.name, x ] }.flatten ]
    =3D=3D>{"dave" =3D> #<struct Person name=3D"dave", age=3D14, city=3D"NY=
C">,
"jane" =3D> #<struct Person name=3D"jane", age=3D6, city=3D"LDN">,
"bob" =3D> #<struct Person name=3D"bob", age=3D12, city=3D"SFO">}