0
할머니 질문에 이어 여기서 조언을 듣고 클래스로 카운터를 보완하고 싶습니다.Ruby의 클래스 오류 - NoMethod
class Counter
counter = 0
end
counter
는 지역 변수와 사라집니다 : 당신이 다음 작업을 수행 할 때 Deaf Grandma
내가
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>'