From: "Simon Kröger" Date: 2006-06-23T04:09:01+09:00 Subject: Re: Turning an Array into a Hash based on an attribute value Ashley Moran wrote: > A guy I work with has some some work on an ActiveRecord class that > basically reads static lookup data, and contains (among other things) an > "identifier" attribute. Given an array of all these objects he wanted > to access them by the contents of the identifier attribute, eg: > > > [1, "2", :three].hash_on(:class) > => {:String=>"2", :Symbol=>:three, :Fixnum=>1} > > Is there a neater (one-line?) way I missed? > > Ashley class Array def hash_on(method) inject({}){|h, i| h.merge({i.send(method).to_s.to_sym, i})} end end cheers Simon