My instance variables gets turned back to nil, even though it was set in a separate function that is called.
I've tried printing out before and after values for the instance variable, and have seen where it turns to nil. It's quite puzzling though. Attaching an example (https://repl.it/repls/FirebrickPresentKeyboard) also below:
class Test
def process
return if a.nil? && b.nil?
puts @some
end
def a
@some = nil
return true
end
def b
@some = "abc"
return false
end
end
class Test2
def process
return if c.nil?
puts @hello
end
def c
@hello = "hello"
return true
end
end
t = Test.new
t.process
t2 = Test2.new
t2.process
In the Test class, I expect @some to print "abc" since it is set during the "b" function. However, it prints nil.
In the Test2 class, I expect @hello to print "hello" and it certainly does.
Test2is needed, as the question is perfectly clear without it. If anything it muddles the question. Note that there is no need for the keywordreturnin the last lines ofaandb. If removed,trueandfalsewould be last expressions evaluated in each method, and therefore would be the respective return values.returndoes no harm, but the practice is to not use it when it is superfluous.