From: Brian Candler Date: 2005-07-13T17:47:43+09:00 Subject: Re: accessing index inside map On Wed, Jul 13, 2005 at 01:46:17AM +0900, nobu.nokada@softhome.net wrote: > > However, perhaps 'to_enum' could be called something friendlier, e.g. > > > > obj.using(:each_with_index).inject(0) {|sum,(a,i)| sum + a*i} > > `using' sounds too general. > > > In fact, even > > > > obj.enum(:each_with_index).inject(0) {|sum,(a,i)| sum + a*i} > > > > 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. > > Enumerable#to_enum has an alias named as #enum_for. The preposition "for" doesn't really sound right here. I don't think we are enumerating "for" each_with_index; we are enumerating "with" each_with_index, or "using", or perhaps "over". The underscore also seems to break up the line visually. Now I know the full details, I can understand the following, but there are two visual breaks between the 'map' and the object being enumerated over: obj.enum_for(:each_with_index).map { .. } ^ ^ break break I think that obj.enum(:each_with_index).map { .. } is more clearly a single method call separating the object and its map. The idea of an enumeration method returning an Enumerator if called without a block is interesting: obj.each_with_index.map { .. } although this means that every enumeration method you write needs extra code to support this behaviour. And I dread to think what happens if you start to chain these methods :-) This mechanism requires every programmer to do more work in their enumeration methods. In that case, I still prefer to modify every method in Enumerable instead: i.e. obj.map(:each_with_index) { .. } But only one person has commented on this so far. Regards, Brian.