From: "David A. Black" Date: 2005-07-12T02:58:47+09:00 Subject: Re: accessing index inside map --927295978-88016008-1121104697=:17833 Content-Type: MULTIPART/MIXED; BOUNDARY="927295978-88016008-1121104697=:17833" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --927295978-88016008-1121104697=:17833 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Mon, 11 Jul 2005, [iso-8859-1] "Pe=F1a, Botp" wrote: > dblack@wobblini wrote: > > #> OK, i'll be shooting for the moon here. How about passing a parameter?= Is > it > #> then possible to make #each or #map same behavior w #each_with_index o= r > #> #map_with_index or whatever *_with_index so that the ff works, > #> > #> def each(with_index=3Dtrue) do... end > #> > #> def map(with_index=3Dtrue) do ... end > #> > #> and thus, we can do > #> > #> [1,2,3,4,5,6].map(true) {|x,i|[2,5].include?(i) ? x : x*2} > # > #I'd hope not to see that. I'm actually hoping that maybe someday all > #the boolean flag methods (like instance_methods(true) etc.) will be > #changed :-) They've always seemed to me to be the most cryptic thing > #in all of Ruby. > # > > Yes, you are correct. But there is some sort of give and take here or > balance if i may say (i think i heard the word balance fr you, sir David)= =2E. > > How about > > [1,2,3,4,5,6].map(WITH_INDEX) {|x,i|[2,5].include?(i) ? x : x*2} > or > [1,2,3,4,5,6].map(with_index=3D>true) {|x,i|[2,5].include?(i) ? x : x*2} > > if we don't pass flags or attribs, wouldn't that make creating additional > methods like > > each > each_with_index > map > map_with_index > collect > collect_with_index > ... > > ? > > it's with_index es all over.. I don't know why, at least not the "..." part. Matz has always considered each case of a possible new method separately, on its own merits. (And collect_with_index would presumably be just an alias of map_with_index :-) I don't think there has to be some automatic trigger where lots of other _with_index methods had to come into existence, unless there were cases where there was a good reason for them. My main argument in favor of map_with_index is actually not symmetry with each_with_index, but the fact that so many of us have had to write that method. I don't have an exact formula, but it's struck me over the years (as with singleton_class) that there's a case to be made on those grounds for adding it to Array. David --=20 David A. Black dblack@wobblini.net --927295978-88016008-1121104697=:17833-- --927295978-88016008-1121104697=:17833--