Another Rails newbie here :) stuck on a section of Agile Web Development with Rails. The book says: def total_price @items.sum { |item| item.price } end However this code throws this error. undefined method `sum'' for #<Array:0x2afcd71feb78> Extracted source (around line #13): 10: 11: <tr class="total-line"> 12: <td colspan="2">Total</td> 13: <td class="total-cell"><%= number_to_currency(@cart.total_price) %></td> 14: </tr> 15: 16: </table> A friend gave me an alternative which does work. def total_price @items.inject(0) { |sum,x| sum += x.price } end I''d really like to know though why the books code does not work. -- 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 -~----------~----~----~----~------~----~------~--~---
On 10/4/07, Peter Heywood <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Another Rails newbie here :) stuck on a section of Agile Web Development > with Rails. The book says: > > def total_price > @items.sum { |item| item.price } > end > > However this code throws this error. > > undefined method `sum'' for #<Array:0x2afcd71feb78> > > Extracted source (around line #13): > > 10: > 11: <tr class="total-line"> > 12: <td colspan="2">Total</td> > 13: <td class="total-cell"><%= number_to_currency(@cart.total_price) > %></td> > 14: </tr> > 15: > 16: </table> > > A friend gave me an alternative which does work. > > def total_price > @items.inject(0) { |sum,x| sum += x.price } > end > > I''d really like to know though why the books code does not work.Not sure. Enumerable#sum is not part of standard Ruby, it''s an ActiveSupport extension, but you should have it if you are running rails. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.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 -~----------~----~----~----~------~----~------~--~---
On 4 Oct 2007, at 20:40, Rick DeNatale wrote:> > On 10/4/07, Peter Heywood <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> >> I''d really like to know though why the books code does not work. > > Not sure. Enumerable#sum is not part of standard Ruby, it''s an > ActiveSupport extension, but you should have it if you are running > rails. >It''s an activesupport extension, which was introduced with rails 1.2. If you''re not using rails 1.2, then you''ve got your explanation Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 10/4/07, Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On 4 Oct 2007, at 20:40, Rick DeNatale wrote: > > > > > On 10/4/07, Peter Heywood <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > >> > >> I''d really like to know though why the books code does not work. > > > > Not sure. Enumerable#sum is not part of standard Ruby, it''s an > > ActiveSupport extension, but you should have it if you are running > > rails. > > > It''s an activesupport extension, which was introduced with rails 1.2. > If you''re not using rails 1.2, then you''ve got your explanationWell I am, maybe Peter isn''t <G> -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On 4 Oct 2007, at 20:40, Rick DeNatale wrote: > >> >> On 10/4/07, Peter Heywood <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >>> >>> I''d really like to know though why the books code does not work. >> >> Not sure. Enumerable#sum is not part of standard Ruby, it''s an >> ActiveSupport extension, but you should have it if you are running >> rails. >> > It''s an activesupport extension, which was introduced with rails 1.2. > If you''re not using rails 1.2, then you''ve got your explanation > > FredFred rails-1.2.3 I don''t suppose the Ruby version matters but here it is ruby 1.8.5 (2006-08-25) [x86_64-linux] -- 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 -~----------~----~----~----~------~----~------~--~---
Rick Denatale wrote:> On 10/4/07, Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> > ActiveSupport extension, but you should have it if you are running >> > rails. >> > >> It''s an activesupport extension, which was introduced with rails 1.2. >> If you''re not using rails 1.2, then you''ve got your explanation > > Well I am, maybe Peter isn''t <G> > > > -- > Rick DeNatale > > My blog on Ruby > http://talklikeaduck.denhaven2.com/Hi Rick Many months ago you helped with a var problem "eRuby" I was having which you solved with "from.untaint" I''m slowly working my way into Rails. Not being a programmer I love the Rails does thing magically. But it may be too much of a good thing ;) -- 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 -~----------~----~----~----~------~----~------~--~---