From: Ashley Moran Date: 2006-06-23T03:50:59+09:00 Subject: Turning an Array into a Hash based on an attribute value 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: data_items = DataItem.find(:all) # data_items[3].identifier => "my_identifier" data_items[:my_identifier].do_something # => data_items[3].do_something The best I can come up with is this: class Array def hash_on(method) method = method.to_sym hash = { } self.each do |i| hash[i.send(method).to_s.to_sym] = i end hash end end > [1, "2", :three].hash_on(:class) => {:String=>"2", :Symbol=>:three, :Fixnum=>1} Is there a neater (one-line?) way I missed? Ashley