# Get total of values in array

**URL:** <https://www.ruby-forum.com/t/get-total-of-values-in-array/151361>\
**Category:** Rails\
**Created:** [October 20, 2008, 6:00pm UTC](https://www.ruby-forum.com/t/get-total-of-values-in-array/151361 "2008-10-20T18:00:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cogrady2k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@cogrady2k](https://www.ruby-forum.com/u/cogrady2k)\
**Post date:** [October 20, 2008, 6:00pm UTC](https://www.ruby-forum.com/t/get-total-of-values-in-array/151361/1 "2008-10-20T18:00:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cogrady2k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@cogrady2k](https://www.ruby-forum.com/u/cogrady2k)\
**Post date:** [October 20, 2008, 6:09pm UTC](https://www.ruby-forum.com/t/get-total-of-values-in-array/151361/2 "2008-10-20T18:09:28Z")

</div>

total = @array.inject(0) { |sum, value| sum + value }

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

Regards,  
Craig
