From: nobuyoshi nakada Date: 2005-10-27T17:46:17+09:00 Subject: Re: Cleaner syntax for .map (is there already a way, or ruby2 idea?) Hi, At Thu, 27 Oct 2005 15:05:41 +0900, Ron M wrote in [ruby-talk:162876]: > That sure beats C, but wouldn't it be nice if there > were a shorthand for doing so. I think something > like this would be very nice: > > people[*].email_addr 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