我有这个代码:
class A
attr_accessor :count
def initialize
@count = 0
end
def increase_count
count += 1
end
end
A.new.increase_count
它抱怨:
in `increase_count': undefined method `+' for nil:NilClass (NoMethodError)
如果我将increase_count
定义更改为:
class A
def increase_count
@count += 1
end
end
然后它不会抱怨。可能是我遗漏了一些东西,或者这只是 Ruby 的一种奇怪行为。