From: Pit Capitain Date: 2002-08-04T23:30:40+09:00 Subject: Verbose map (Re: Should this work?) On 4 Aug 2002, at 22:31, Gavin Sinclair wrote: > (...) > Now, I love the "map" method, but sometimes its use is more verbose > than it could be. It "would be nice" if one could do the following > things: > > names = %w(john mary adam jacky) > names.map { capitalize } # -> ["John", "Mary", "Adam", > "Jacky"] > > x, y, z = [], [], [] > [x, y, z].map { $_ << [nil] } # -> [nil, nil, nil] > [x, y, z].map { $_ == [nil] } # -> [true, true, true] > > Obviously there are limits, but I think that there could be a way to > do this without exposing the language to ambiguity, contradiction, > slothfulness or confused identity (!) The "capitalize" example may be > stretching it a bit far, but surely the other two examples are good? > > What do people think? What about the following? It's not quite the same, but less verbose than map itself: module Enumerable def mapsend( method, *args ) map { | target | target.send( method, *args ) } end end names = %w(john mary adam jacky) names.mapsend( :capitalize ) # -> ["John", "Mary", "Adam", "Jacky"] x, y, z = [], [], [] [x, y, z].mapsend( :<<, nil ) # -> [[nil], [nil], [nil]] [x, y, z].mapsend( :==, [nil] ) # -> [true, true, true] Regards, Pit