<分区>
我不明白为什么会出现 nil 错误。我正确地创建了二传手。但它不接受 -=、+= 或 = 运算符后面的本身。为什么?
class Test
def var; @var || 0; end
def var=(value)
@var = value
end
def initialize
@var = 2.4 # Sample value
end
def test
puts var
var -= 1 # <<< crash: undefined method for nil class
puts var
var = var - 1 # <<< crash: undefined method for nil class
puts var
end
end
a = Test.new
a.test