Given this code:
class Something
attr_accessor :my_variable
def initialize
@my_variable = 0
end
def foo
my_variable = my_variable + 3
end
end
s = Something.new
s.foo
I get this error:
test.rb:9:in `foo': undefined method `+' for nil:NilClass (NoMethodError)
from test.rb:14:in `<main>'
If attr_accessor
creates a method called my_variable
(and ..=), why can't foo
find the method? It works if I change it to self.my_variable
, but why? Isn't self
the default receiver?