From: Daniel Schierbeck Date: 2005-10-28T23:07:05+09:00 Subject: Re: Cleaner syntax for .map (is there already a way, or ruby2 idea?) David A. Black wrote: > Hi -- > > On Fri, 28 Oct 2005, Daniel Schierbeck wrote: > >> Daniel Schierbeck wrote: >> >>> David A. Black wrote: >>> >>>> I know that people.every could return >>>> some kind of generator or enumerator, which could then be fed >>>> "email_addr" symbolically... >>> >>> >>> >>> >>> module Enumerable >>> def every >>> enum, obj = self, Object.new >>> obj.define_method :method_missing do |name, *args| >>> enum.map { |element| element.send(name, *args) } > > > If you must do this, you'd probably want each rather than map there. Nope. I wan't it to return an array of the values returned. ["john", "sylvia", "sarah"].every.upcase -> ["JOHN", "SYLVIA", "SARAH"] That's of course not a very good example. This would probably be better: addresses = contacts.every.email_addr As opposed to addresses = contacts.collect { |contact| contact.email_addr } But I agree that the dot syntax is bad, I was just proving that it could easily be done. This would be better: addresses = contacts.every(:email_addr) And maybe even have a `with_every' method: contacts.with_every(:email_addr) do |email_addr| puts " - " + email_addr end Which could be implemented this way module Enumerable def with_every(*args) each do |element| yield element.send(*args) end end end Cheers, Daniel