I am creating a method that will take an array of numbers and add them together. I don't want to use inject
because I haven't learned it yet. I prefer to start with the basics. I want to use either each
or while
.
I've been re-writing this code and testing it against rspec
, and I keep running into a problem because the first test consists of the array being empty with nil
. I tried doing an if else
statement to set nil
to 0
if the array is empty?
, but that didn't seem to work. Here is what I've got right now.
def sum(x)
total = 0
sum.each { |x| total += x}
total
end
The rspec
is testing an empty array []
as well as others that have multiple integers. Thoughts?