I am just trying to understand the behaviour of the destroy
method in the following code:
UPDATE : Please note my intention is to understand the behaviour, not the immediate solution for assigning nil to the variable.
def conf
@conf ||= { 'foo' => { 'bar' => 'baz' } }
end
def destroy
conf = nil
end
def change
conf['foo']['bar'] = 'meh'
end
def add
conf['foo']['abc'] = 'moo'
end
Here is the output for invoking the add
method:
add
pp conf
# {"foo"=>{"bar"=>"baz", "abc"=>"moo"}}
change
method
change
pp conf
# {"foo"=>{"bar"=>"meh"}}
destroy
method
destroy
pp conf
# {"foo"=>{"bar"=>"baz"}}
So, why doesn't destroy
result in conf
having nil
?
Another related snippet, this time with a scalar not a hash:
def foo
@foo ||= "bar"
end
def destroyfoo
foo = nil
end
def changefoo
foo = "baz"
end
Result when calling changefoo
and destroyfoo
both:
destroyfoo
puts foo
# "bar"
...
changefoo
puts foo
# "bar"
Any pointers on what might be happening would be useful in both cases.