I am trying to write a small function with ruby that gets fed a array from the user and then sums up the data in the array. I have written it as
def sum(a)
total = 0
a.collect { |a| a.to_i + total }
end
The function then runs through a rspec, which tests it by initially feeding into it a blank array. This this causes the following error
sum computes the sum of an empty array
Failure/Error: sum([]).should == 0
expected: 0
got: [] (using ==)
So its telling me that when it feeds the blank array in, its supposed to get 0 but instead its getting the array. I tried putting in a if statement written as
def sum(a)
total = 0
a.collect { |a| a.to_i + total }
if total = [] then { total = 0 end }
end
but it gives me an error saying
syntax error, unexpected '}', expecting => (SyntaxError)
what am I doing wrong?