Ruby 程序错误:NoMethodError

发布于 2024-09-09 22:45:28 字数 722 浏览 4 评论 0原文

我为教程问题输入了一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴者 2024-09-16 22:45:28

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 sure Automatically check for updates is checked.

Select Cutting-Edge in the Watch For: dropdown menu. Finally, click Check Now. The latest release (r1589) should automatically download. Interactive input is re-enabled in this release.

一曲琵琶半遮面シ 2024-09-16 22:45:28

如果您在 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, so gets returns nil.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文