From: Stefano Crocco Date: 2008-10-03T21:22:16+09:00 Subject: Re: Iterate over array and add numbers of each object Alle Friday 03 October 2008, Joshua Muheim ha scritto: > Hi all > > number = 0; > objects.each do |o| > number += o.some_number > end > return number > > I guess there's a much more beautiful way for this, or isn't? Something > like collect or so... > > Thanks :-) > Josh If objects is an array, or any other thing which mixes-in the Enumerable module, you can use inject: objects.inject(0){|tot, o| tot + o.some_number} Stefano