def room_actions
case $next_move
when "instructions"
instructions
when $next_move.any? { |x| ['up', 'down', 'left', 'right'].include?(x) }
determine_start
else
puts "I don't understand that."
prompt; $next_move = gets.chomp()
room_actions
end
end
请原谅我不明白这里发生了什么,但为什么 ruby 会抛出这个错误?
foo.rb:83:in
room_actions': undefined method
any?' for "":String (NoMethodError)
$next_move
是一个全局变量(我知道它们不好,我正在重构),我从状态机中的 gets chomp 将其输入此方法。
看来我没有定义“任何?”方法。或者我传递的字符串是空的?无论如何,不是'.any'吗?内置的 ruby 方法?我正在使用 2.0.0。
感谢您的浏览和建议。
最佳答案
字符串(无论是否为空)都没有any?
方法(可枚举的方法,例如数组或散列。字符串在 ruby 1.8 中是可枚举的,但不再是可枚举的)。
我很不清楚为什么你首先要尝试调用 .any?
- 如果你想检查它是否是允许值列表中的一个,那么
['up','down','left','right'].include?($next_move)
会做这份工作
关于ruby - '.any?' :String (NoMethodError 的未定义方法 "",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17005193/