From: Pit Capitain Date: 2005-10-28T00:48:04+09:00 Subject: Re: [SUMMARY] Ducksay (#52) Ruby Quiz schrieb: > > class String > def width > inject(0) {|w, line| [w, line.chomp.size].max } > end > end > > I just know I would have written width() as: > > map { |line| line.chomp.size }.max > > Dave's is more efficient though. It only ever keeps the highest number, instead > of building an Array of all the lengths. Just one more reason inject() is the > one iterator to rule them all. Thanks for trick #562, Dave! I'm not sure about the efficiency. The inject() version builds a new array on each pass, whereas the map() version builds only one array. Even an inject() implementation without the arrays was running slower than the map() implementation on my computer. inject() is nice, but it definitely doesn't rule them all (sorry Robert :-). Regards, Pit