From: Sebastian Hungerecker Date: 2007-11-21T02:24:55+09:00 Subject: Re: Quick way to get attributes (without using collect) Max Williams wrote: > This was inspired by Rails, but i think it's common Ruby question. > [...] > 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} In rails you can do "employee_names = employees.collect(&:name)" which is somewhat more concise (though it incurs a performance penalty, I am told). > Is there a way to set it up so that > .foos > is automatically treated as > .collect { |x| x.foo } module Enumerable def method_missing(meth, *args, &blk) map {|x| x.send(meth,*args, &blk)} end end This might easily lead to bugs though if you want to invoke method on the elements of the enum, not considering or not knowing that the enum itself implements that method as well. For example: [2,4,6] / 2 #=> [1, 2, 3] But: [2,4,6] * 2 #=> [2,4,6,2,4,6] Or: ["1","2","3"].to_i #=> [1, 2, 3] But: [1,2,3].to_s #=> "123" HTH, Sebastian -- Jabber: sepp2k@jabber.org ICQ: 205544826