0
繼續從奶奶問題開始,我想在這裏接受他們的建議,並將計數器作爲一個類來使用。 Deaf GrandmaRuby中的類錯誤 - NoMethod
這是我在
puts 'Say something nice to Grandma.'
puts 'You may need to shout > '
class Counter
counter = 0
def Plus
counter += 1
end
def Minus
counter -= 1
end
def Reset
counter = 0
end
end
MyCounter = Counter.new
def speaks()
$speak = gets.strip
if $speak != 'Bye'
talk()
else
exitPlan()
end
end
def talk()
if $speak == $speak.downcase
puts 'Huh Speak up Sonny'
else
year = rand(1930..1951)
puts 'No not Since ' + year.to_s
end
MyCounter.Minus
if counter < 0
Counter.reset
end
puts 'Say something nice to Grandma'
speaks()
end
def exitPlan()
MyCounter.Plus
unless counter == 3
puts 'Say something nice to Grandma'
speaks()
else
puts 'good night Sonny'
end
end
speaks()
這哪裏是NoMethod錯誤
C:\Users\renshaw family\Documents\Ruby>ruby gran2.rb
Say something nice to Grandma.
You may need to shout >
Hi
No not Since 1939
gran2.rb:10:in `Minus': undefined method `-' for nil:NilClass (NoMethodError)
from gran2.rb:35:in `talk'
from gran2.rb:22:in `speaks'
from gran2.rb:52:in `<main>'