我刚开始学习Ruby而遇到了一个问题:
def gas
@speed += @velocity if @speed < @max_speed
end
'gas'中的:未定义的方法`&lt;'对于nil:NilClass(NoMethodError)
答案 0 :(得分:4)
@speed
中的if @speed
未定义或nil
值。
试试这个:
def gas
@speed ||= 0
@speed += @velocity if @speed < @max_speed
end
或者,在其他位置初始化@speed
一些值(可能在initialization
。