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.
D13563098fc07b07400f833c4e6e7169?d=identicon&s=25 Roger Reeks (cogrady2k)
on 2008-10-20 18: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
B14575f0ca69f10938fdd67e7156e0e1?d=identicon&s=25 Craig Demyanovich (Guest)
on 2008-10-20 18: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.