我无法确定为什么这不起作用.我把它扔进了irb,它工作得很好..如果你看到这个问题,请告诉我
我的代码:
class Player
def play_turn(warrior)
@warrior = warrior
@turn ||= 0
puts @warrior.look
if @warrior.look.include?("Wizard")
@warrior.shoot!
Run Code Online (Sandbox Code Playgroud)
put的输出是一个数组:
nothing
Captive
Wizard
Run Code Online (Sandbox Code Playgroud)
由于某种原因,它不会拍摄,这个if语句返回false ..谢谢!
@warrior.look返回一个对象数组Space而不是strings.只puts将它们转换为string这就是你.include?("Wizard")不工作的原因.
因此,您可以在将Space对象转换为字符串之前将其转换为字符串, .include?("Wizard")或者您需要一个Space对象使用的条件,例如使用Space#enemy?或Space#unit方法:
if @warrior.look.any? { |space| space.enemy? }
@warrior.shoot!
Run Code Online (Sandbox Code Playgroud)