Another Rails newbie here stuck on a section of Agile Web D.
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:
12: Total |
13: <%= number_to_currency(@cart.total_price)
%> |
14:
15:
16:
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.
On 10/4/07, Peter H. [email protected] wrote:
undefined method `sum’ for #Array:0x2afcd71feb78
16:
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/
On 4 Oct 2007, at 20:40, Rick DeNatale wrote:
On 10/4/07, Peter H. [email protected] 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
On 10/4/07, Frederick C. [email protected] 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
–
Rick DeNatale
My blog on Ruby
http://talklikeaduck.denhaven2.com/
Rick Denatale wrote:
On 10/4/07, Frederick C. [email protected] 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
–
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
Frederick C. wrote:
On 4 Oct 2007, at 20:40, Rick DeNatale wrote:
On 10/4/07, Peter H. [email protected] 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
Fred
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]