From: James Edward Gray II Date: 2005-10-28T00:55:04+09:00 Subject: Re: [SUMMARY] Ducksay (#52) On Oct 27, 2005, at 10:48 AM, Pit Capitain wrote: > 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 :-). I didn't make that very clear. I was speaking of memory consumption. map() has to copy the whole Array, while inject() does not. James Edward Gray II