I am trying to create a CLI arithmetic quiz program and am having difficulty keeping the score within the method. I get the following error message whenever I attempt to increment the score variable (which is declared).
main.rb:17:in `ask': undefined method `+' for nil:NilClass (NoMethodError)
from main.rb:23:in `<main>'
The code is pasted below. I believe I need to use the 'return' statement to do the increment but I am unsure how this works. (Even if this is not the problem, I'd still welcome any help from anyone to explain how you use boolean return values to count.)
def ask(question, answer)
print question
user_answer = gets.chomp
if user_answer == answer
puts "Correct!"
score += 1
else
puts "Wrong! The answer was #{answer}"
end
end