From: Robert Klemme Date: 2005-07-12T20:20:52+09:00 Subject: Re: accessing index inside map Brian Candler wrote: >> IMHO the enumerator approach is slightly superior as your suggestion >> can lead to conflicts with current method arguments as you have >> noticed. Personally I don't have a problem using >> >> a.to_enum(:each_with_index).inject(0) {|sum,(a,i)| sum + a*i} >> >> I think this is more flexible and modularized. And the overhead of >> a new instance creation is usually neglectible compared to the >> iteration overhead. > > "Do the simplest thing which will possibly work" :-) Well, apparently we have differing opinions on this. Personally I find the Enumerator approach simpler as it does not clutter the original's class interface (introducing an optional parameter on each method doubles the number of legal invocations). :-) > Actually I think my version reads better: > > a.inject(0,:each_with_index) {|sum,(a,i)| sum + a*i} > > "Starting with 0, call :each_with_index with the following block". There is a chance that folks who don't know #inject might read "start with index 0". Well, I think a vote is in order. Did you consider submitting this as RCR? Kind regards robert