From: tamouse mailing lists Date: 2013-03-26T14:20:11+09:00 Subject: Re: Choice of methods/implementation (was: Writing a game, trying to make dynamic loops) On Mon, Mar 25, 2013 at 7:03 PM, Henry Maddocks wrote: > On 26/03/2013, at 11:04 AM, Robert Klemme wrote: >> >> I don't very much like the previous Array.new() {} either, although >> being 1-based might be easier for some to understand, but breaks my >> oh-so-long-established zero-based array mentality. I'd have approached >> this with #reduce. I'd appreciate hearing your thoughts on the >> trade-offs. >> >> >> Which previous Array.new {} do you mean? The one with the separate >> counter variable? >> >> I don't think this calls for #inject / #reduce. IMHO #map is a better >> choice here since you do not need to explicitly deal with the Array >> copied into. > > > I like to think of 'reduce' meaning you have several things that you want to > reduce to a single thing whereas with 'map' you are mapping one thing to > another thing, e.g. mapping integers to players. I do probably overuse #reduce, as I find those things elegant. But I do sometimes forget about the #map part of the Map Reduce pattern. I'm getting better though! I recently recoded something from this: names = items.reduce([]) {|m,i| m << i.name} to this: names = items.map(&:name)