From: Brian Adkins Date: 2007-11-21T02:59:59+09:00 Subject: Re: Quick way to get attributes (without using collect) On Nov 20, 12:12 pm, Farrel Lifson wrote: > On 20/11/2007, Max Williams wrote: > > Say > > i have an array of Employee objects, which have an attribute "name". I > > want to get all the names, as an array. > > ... > > What i want to do is something like this - > > > employee_names = employees.names > > > But what i have to keep on doing is something like this - > > > employee_names = employees.collect{ |emp| emp.name} > > You can use the Symbol#to_proc method > > class Symbol > def to_proc > Proc.new {|e| e.send(self)} > end > end > > Which will let you right > employee_names = employees.collect(&:name) map is shorter than collect. Also, adding optional args to to_proc will make it more generally useful. class Symbol def to_proc lambda {|obj, *args| obj.send(self, *args) } end end employee_names = employees.map(&:name) I'd be wary of using method_missing for this. Brian Adkins