# Calculations on arrays?

**URL:** <https://discuss.rubyonrails.org/t/calculations-on-arrays/22946>\
**Category:** rubyonrails-talk\
**Created:** [January 10, 2008, 2:28am UTC](https://discuss.rubyonrails.org/t/calculations-on-arrays/22946 "2008-01-10T02:28:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [January 10, 2008, 2:28am UTC](https://discuss.rubyonrails.org/t/calculations-on-arrays/22946/1 "2008-01-10T02:28:27Z")

</div>

Hi everyone,

I was wondering if there's an easy way to grab the sum of a column in an array in ruby? Something that would work like @posts.sum(:column\_name). I know there's one for Classes, but is there something similar for arrays?

Thanks!

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [January 10, 2008, 2:37am UTC](https://discuss.rubyonrails.org/t/calculations-on-arrays/22946/2 "2008-01-10T02:37:21Z")

</div>

Dave Amos wrote:

> Hi everyone,
> 
> I was wondering if there's an easy way to grab the sum of a column in an array in ruby? Something that would work like @posts.sum(:column\_name). I know there's one for Classes, but is there something similar for arrays?
> 
> Thanks!

Something like this?

total\_comments = posts.inject(0) { |sum, post| sum + post.comments.size }

---

<div class="post-metadata">

**Author:** ![radar](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/radar/32/604_2.png) [@radar](https://discuss.rubyonrails.org/u/radar)\
**Post date:** [January 10, 2008, 2:40am UTC](https://discuss.rubyonrails.org/t/calculations-on-arrays/22946/3 "2008-01-10T02:40:17Z")

</div>

posts.map { |p| p.comments.size }.sum

will also do the trick

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [January 10, 2008, 2:45am UTC](https://discuss.rubyonrails.org/t/calculations-on-arrays/22946/4 "2008-01-10T02:45:38Z")

</div>

Ryan Bigg wrote:

> posts.map { |p| p.comments.size }.sum
> 
> will also do the trick
> 
> > Something like this?
> > 
> > total\_comments = posts.inject(0) { |sum, post| sum + post.comments.size } -- Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > 
> > \>
> 
> -- Ryan Bigg [http://www.frozenplague.net](http://www.frozenplague.net) Feel free to add me to MSN and/or GTalk as this email.

Will that work? Array#sum doesn't exist.

Also, there's a missing "=" character in my previous reply. Use this instead:

total\_comments = posts.inject(0) { |sum, post| sum += post.comments.size }

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [January 10, 2008, 2:47am UTC](https://discuss.rubyonrails.org/t/calculations-on-arrays/22946/5 "2008-01-10T02:47:23Z")

</div>

> Will that work? Array#sum doesn't exist.

Ah, I see. Rails adds it to Enumerable. Nice!

---

<div class="post-metadata">

**Author:** ![Russell\_McConnachie](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@Russell\_McConnachie](https://discuss.rubyonrails.org/u/Russell_McConnachie)\
**Post date:** [January 10, 2008, 2:48am UTC](https://discuss.rubyonrails.org/t/calculations-on-arrays/22946/6 "2008-01-10T02:48:00Z")

</div>

I was going to say:

Posts.find(:all).map(&:column\_name).sum or posts.map(&:column\_name).sum

Although Ryan's example is probably much faster being it doesn't have to do a query if the data is already cached.

I don't have an Ryan Bigg wrote:

---

<div class="post-metadata">

**Author:** ![radar](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/radar/32/604_2.png) [@radar](https://discuss.rubyonrails.org/u/radar)\
**Post date:** [January 10, 2008, 2:58am UTC](https://discuss.rubyonrails.org/t/calculations-on-arrays/22946/7 "2008-01-10T02:58:00Z")

</div>

It will be even faster if you had a counter\_cache on the comments too, just another field for a post record to store the number of comments it has. Built-in feature of Rails.

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [January 10, 2008, 2:58am UTC](https://discuss.rubyonrails.org/t/calculations-on-arrays/22946/8 "2008-01-10T02:58:15Z")

</div>

Wow, thanks! I'll try a couple of these ideas out and see what works best for me.
