【发布时间】:2013-06-08 16:57:24
【问题描述】:
这是一大段代码的 sn-p:
print "> "
$next_move = gets.chomp
case $next_move.include?
when "instructions"
puts "$next_move is instructions"
else
puts "$next_move is NOT instructions"
end
每次我在终端中运行它时,无论我使用的是 ruby 1.8.7、1.9.3 还是 2.0.0,我都会收到以下错误:
test.rb:4:in `include?': wrong number of arguments (0 for 1) (ArgumentError)
from test.rb:4
此代码昨晚在另一台计算机上运行。
include? 不是在检查那个全局变量的内容吗?我应该向它传递什么其他参数?
我有点难过,尤其是因为我所做的只是将代码从一台计算机移动到另一台计算机。
【问题讨论】:
-
"此代码昨晚在另一台计算机上运行。"错误……不。 :-) 很可能,
case "next_move".include? "instructions"做了,或者case "next_move" when "instructions"。 -
使用全局变量时要非常小心:
$next_move。在 Ruby 代码中很少需要它们,而且在大多数情况下,它们是您做错了什么的警告信号。 -
@tinman,谢谢。我已经改进了我的编码以避免它们。这是几周前的一个练习,至今仍让我感到困惑。尽管丹尼斯不相信我,但老实说,我所做的只是在另一台计算机上运行代码,突然之间,那些有效的东西似乎不再有效......