From: "David A. Black" Date: 2005-10-28T23:16:44+09:00 Subject: Re: Cleaner syntax for .map (is there already a way, or ruby2 idea?) Hi -- On Fri, 28 Oct 2005, Daniel Schierbeck wrote: > 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. Oh right, never mind. > ["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 } I strongly prefer the latter. You've gone out of your way to make it long and wordy :-) addresses = contacts.map {|c| c.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) There was an RCR once for enum.map(:method) {|x| ... } but it was rejected. > And maybe even have a `with_every' method: > > contacts.with_every(:email_addr) do |email_addr| > puts " - " + email_addr > end "with_every" feels like the wrong word, though. It suggests that they're all being used at the same time. Maybe: contacts.each_send(:email_addr) or something. David -- David A. Black dblack@wobblini.net