From: daz Date: 2005-07-13T18:25:51+09:00 Subject: Re: accessing index inside map Robert Klemme wrote: > Brian Candler wrote: > > > In fact, even > > > > obj.enum(:each_with_index).inject(0) {|sum,(a,i)| sum + a*i} > > That reads better IMHO. > > > reads better to me, as it's not stressing the creation of an > > intermediate object. to_foo looks like you are converting obj into > > something completely different, rather than just adding a temporary > > wrapper. > > Yeah, maybe. Anyway, this is far better than rename IMHO. > [1]; obj.enum(:each_with_index).inject(0) {|sum,(a,i)| sum + a*i} [2]; i=-1; obj.inject(0) {|sum, a| sum + a*(i+=1)} Can anyone shade me in on what, if any, advantage [1] has over [2]: -- in terms of: a) Readability b) Ease of use and understanding c) Performance d) Flexibility e) Accuracy f) Memory usage g) Anything else you care to throw in ;-) I can tell at a glance that it's going to require more typing because it contains the string '_with_index' ... ## keystrokes (including shifts on UK kbd) # _with_index #-> 11/13 # i=-1; i+=1; #-> 10/11 But there seem to be so many other overheads in comparison, that I can't focus on whether or not you're saying something that I need to get my head around. TIA, daz