From: Hugh Sasse Staff Elec Eng Date: 2003-12-04T05:04:22+09:00 Subject: Re: each_with_what_index? On Thu, 4 Dec 2003, David A. Black wrote: > Hi -- > > On Thu, 4 Dec 2003, Hugh Sasse Staff Elec Eng wrote: > > > Suppose if each_with_index allowed a range parameter > > a.each_with_index(1...a.size) {|t,i|...} > > Would that be worth an RCR? It looks sort of natural to me, but... > > Just for fun: > > module Enumerable > def each_with_index(range=(0...size)) > range.each {|i| yield at(i),i} > end > end Easier than I thought. > > though it doesn't do much for me. I guess I haven't seen the problem > to which it would be a solution, and it seems to be based on a sort of One place it could be used is with one array indexing k others, where the indices are sorted based on {whatever happens to be of interest}. But it is anything that responds_to?(each) so there may be more applications than are dreamt of in my philosophy... > rollback of the original request (i.e., here's a enumerable; give me > its elements and their indices; wait, actually, don't act on the > receiver but on a subsequent restraint...). It feels inside-out to > me. Do act on the receiver but... "I want this book printed, but just the odd pages (then I'll feed them through again)..." I think that was almost how the problem was framed: "I want all the elements, well, except the zeroth, of course!". It does make it easier to change your mind about which ones, later. > > > David > -- > David A. Black > dblack@wobblini.net > Hugh