Ruby 程序错误:NoMethodError
我为教程问题输入了一个简单的 Ruby 代码,如下所示。
#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,应该没问题。有什么解决办法吗?
太感谢了 :)
I typed up a simple Ruby code for a tutorial question, as shown below.
#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!"
However, when I run this, the window displays:
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
I don't understand why chomp
is not recognized. I'm using textMate on a Mac I have Ruby version 1.8.7, which should be fine. Any solutions?
Thank you so much :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Adrian 关于 TextMate 1.5.9 (r1510) 中禁用交互式输入的说法是正确的。 请参阅 TextMate 开发人员的这篇文章。
但是,您可以升级到“尖端”TextMate 版本,该版本可以恢复交互式输入,并允许您很好地运行上述代码。转到 TextMate 的
Preferences ->软件更新
并确保选中自动检查更新
。在
Watch For:
下拉菜单中选择Cutting-Edge
。最后,点击立即检查
。最新版本 (r1589) 应该会自动下载。此版本中重新启用了交互式输入。Adrian is right about interactive input being disabled in TextMate 1.5.9 (r1510). See this post from TextMate's developer.
However, you can upgrade to a "cutting-edge" TextMate release that restores interactive input, and will allow you to run the above code just fine. Go to TextMate's
Preferences -> Software Updates
and make sureAutomatically check for updates
is checked.Select
Cutting-Edge
in theWatch For:
dropdown menu. Finally, clickCheck Now
. The latest release (r1589) should automatically download. Interactive input is re-enabled in this release.如果您在 TextMate 中使用 Cmd-R 快捷键来运行代码,您将无法为其提供输入,因为 textmate 仅支持输出。您必须在终端中运行它。您收到该错误的原因是
$stdin
已关闭,因此gets
返回nil
。If you are using the Cmd-R shortcut in TextMate to run your code, you will not be able to supply it input because textmate only supports output. You will have to run it in a terminal instead. The reason you are getting that error is because
$stdin
is closed, sogets
returnsnil
.