From: Sean O'Halpin Date: 2005-10-27T19:32:24+09:00 Subject: Re: Cleaner syntax for .map (is there already a way, or ruby2 idea?) On 10/27/05, nobuyoshi nakada wrote: > What about: > > module Mappable > class Mapper > def initialize(obj) > @obj = obj > end > def method_missing(meth, *args, &block) > @obj.map {|i| i.send(meth, *args, &block)} > end > end > def self.included(klass) > super > aref = klass.instance_method(:[]) > klass.module_eval do > define_method(:[]) do |*idx| > if idx.empty? > Mapper.new(self) > else > aref[*idx] > end > end > end > end > end > > class Array > include Mappable > end > > a = %w[a b c] > p a[].upcase > > -- > Nobu Nakada > As always, there's something magical about your code! Unfortunately, it makes pp blow up for one ;) For a similar purpose, I use this - not as slick but simple and understandable. Doesn't handle the nesting though. module Enumerable def where(&block) self.select{|x| x.instance_eval(&block) } end def project(&block) self.map{|x| x.instance_eval(&block) } end end as in depts.where{ name == "Apps" }.project{ people.project{ name } } Regards, Sean