Topic: Query using do..end
I am new to Ruby and was trying out some snippets of code. I was wondering why the following code would yield me such an error.
intArr.each do |item|
adds = adds + item
arrays.rb:5:in `addArray': undefined method `+' for nil:NilClass (NoMethodError)
from arrays.rb:4:in `each'
from arrays.rb:4:in `addArray'
However, if I remove the comment on #adds=0 it works fine. What exactly is the meaning of that error? I read somewhere that it means that the method '+' was called on nil. So does that mean that because adds was never initialized (and hence being nil) this error comes up? If that is the case, do we have to actually initialize all variables before their usage? Or is there some other way to do this?