From: "David A. Black" Date: 2005-07-11T20:07:37+09:00 Subject: Re: accessing index inside map --927295978-895367563-1121080026=:1059 Content-Type: MULTIPART/MIXED; BOUNDARY="927295978-895367563-1121080026=:1059" 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-895367563-1121080026=:1059 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Mon, 11 Jul 2005, Yukihiro Matsumoto wrote: > Hi, > > In message "Re: accessing index inside map" > on Mon, 11 Jul 2005 10:50:31 +0900, "Pe=F1a, Botp" writes: > > |# (1..6).each_with_index.map{|x,i|[2,5].include?(i) ? x : x*2} > |# > |#then. It's much simpler isn't it? > > |but isn't > | each_with_index.map > | > |so close to being > | map_with_index > | > |? > > Yes. But when we add map_with_index, we might be asked to add > collect_with_index, detect_with_index, inject_with_index, and all > other enumerable methods _with_index as well. I'm probably the person who has asked for map_with_index the most, and I promise not to ask for *_with_index :-) I can understand that a general solution makes sense, though I'm still very unclear on the whole notion of "index" as it pertains to Enumerable. As for: (1..6).each_with_index.map{|x,i|[2,5].include?(i) ? x : x*2} it is simpler than enum_for, but I wonder if it's equally clear. It seems that one would have to memorize a list of methods that behave this way, rather than really seeing it directly in the code. Also, would one have to define, say, #each to return an Enumerator, if one were writing a new class and mixing in Enumerable? David --=20 David A. Black dblack@wobblini.net --927295978-895367563-1121080026=:1059-- --927295978-895367563-1121080026=:1059--