Forum: Ruby on Rails get total of values in array

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Roger R. (Guest)
on 2008-10-20 20:00
is there an easy way to add all thevalues stored in an array together?

i tried total = @array.total  but i dont think thats right?

thanks for any help
Craig D. (Guest)
on 2008-10-20 20:09
(Received via mailing list)
total = @array.inject(0) { |sum, value| sum + value }

The inject method comes from the Enumerable module, which Array
includes.
Docs are here: http://ruby-doc.org/core/classes/Enumerable.html#M003160
.

Regards,
Craig
This topic is locked and can not be replied to.