You forgot to use @
so a
is interpreted as a local variable which in this case has no value yet.
···
Le 4 juil. 2018 14:06, dade dimapg@rambler.ru a écrit :
Hello, let’s look at an example
class A
attr_accessor :a
def initialize(a)
@a = a
end
def method1(val)
a = val
end
def method2(val)
a = a + val
end
end
a = A.new(1)
a.method1(2) #=> 3
a.method2(2) #=> undefined method `+’ for nil:NilClass
Why the second a is nil?
dade.