我爲教程問題鍵入了一個簡單的Ruby代碼,如下所示。Ruby程序錯誤:NoMethodError
#Grandma is deaf!
puts "Hey Sonny! It's your lovely Grandmother! How are you?"
response = gets.chomp
while response != "BYE"
if response != response.upcase
puts "Huh?! I CAN'T HEAR YOU!"
else
puts "NO! NOT SINCE " + (1930 + rand(21)).to_s + "!"
end
response = gets.chomp
end
puts "GOOD BYE, SONNY!"
然而,當我運行此,該窗口顯示:
Hey Sonny! It's your lovely Grandmother! How are you?
NoMethodError: private method ‘chomp’ called for nil:NilClass
at top level in deafGrandma.rb at line 3
我不明白爲什麼chomp
無法識別。我在Mac上使用textMate我有Ruby版本1.8.7,應該沒問題。任何解決方案
太謝謝你了:)
您應該編輯您的問題並用四個空格縮進代碼以保留縮進/格式。 – Adrian 2010-07-17 22:09:25