我正在对数组成员(浮点数)进行数学运算。类型看起来正确。我仍然得到奇怪的错误。根本没有nil
价值。这是什么错误?
nil can't be coerced into Float
步骤1
newFront = [412.5, 312.5]
@direction = [1.0, 0.0]
@length = 50.0
retRear = [newFront[0] - (@direction[0] * @lenght), newFront[1] - (@direction[1] * @lenght)]
# => TypeError: nil can't be coerced into Float
# from (irb):13:in `*'
# from (irb):13
# from /usr/bin/irb:12:in `<main>'
第2步
newFront[0].class # => Float
@direction[0].class # => Float
@length.class # => Float
第 3 步
nfx = Float(newFront[0]) # => 412.5
dx = Float(@direction[0]) # => 1.0
nfy = Float(newFront[1]) # => 312.5
dy = Float(@direction[1]) # => 0.0
@l = 50.0
retRear = [nfx - (dx * @l), nfy - (dy * @l)] # => [362.5, 312.5]
这就是我想要的。Ruby 是否想告诉我我根本不能使用数组进行浮点运算?此外,将相同的表达式重写为一个表达式也失败了。
retRear = [Float(newFront[0]) - (Float(@direction[0]) * Float(@lenght)), Float(newFront[1]) - (Float(@direction[1]) * Float(@lenght))]
# => TypeError: can't convert nil into Float
# from (irb):78:in `Float'
# from (irb):78
# from /usr/bin/irb:12:in `<main>'