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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 } -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
posts.map { |p| p.comments.size }.sum will also do the trick On Jan 10, 2008 1:07 PM, Jeremy Weiskotten <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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 > } > -- > Posted via http://www.ruby-forum.com/. > > > >-- Ryan Bigg http://www.frozenplague.net Feel free to add me to MSN and/or GTalk as this email. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ryan Bigg wrote:> posts.map { |p| p.comments.size }.sum > > will also do the trick > > On Jan 10, 2008 1:07 PM, Jeremy Weiskotten > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > >> >> Something like this? >> >> total_comments = posts.inject(0) { |sum, post| sum + post.comments.size >> } >> -- >> Posted via http://www.ruby-forum.com/. >> >> > >> > > > -- > Ryan Bigg > 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 } -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> Will that work? Array#sum doesn''t exist. >Ah, I see. Rails adds it to Enumerable. Nice! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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:> posts.map { |p| p.comments.size }.sum > > will also do the trick > > On Jan 10, 2008 1:07 PM, Jeremy Weiskotten > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org > <mailto:rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>> wrote: > > > 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 > } > -- > Posted via http://www.ruby-forum.com/. > > http://www.frozenplague.net > Feel free to add me to MSN and/or GTalk as this email. > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. On Jan 10, 2008 1:18 PM, Russell McConnachie <russell-chlIOe/xi6aw5XvHYEYAbQ@public.gmane.org> wrote:> > 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: > > posts.map { |p| p.comments.size }.sum > > > > will also do the trick > > > > On Jan 10, 2008 1:07 PM, Jeremy Weiskotten > > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org > > <mailto:rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>> wrote: > > > > > > 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 > > } > > -- > > Posted via http://www.ruby-forum.com/. > > > > http://www.frozenplague.net > > Feel free to add me to MSN and/or GTalk as this email. > > > > > > > >-- Ryan Bigg http://www.frozenplague.net Feel free to add me to MSN and/or GTalk as this email. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Wow, thanks! I''ll try a couple of these ideas out and see what works best for me. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---