I clearly have either forgotten something important, or I never
understood it correctly, but I cannot sort out what is wrong with the
following:
module AO_Map
. . .
def ao_map_module_method( y )
puts( current_batch.to_yaml )
. . .
end
class Main
. . .
require file_dir + “/ao_map”
include AO_Map
. . .
attr_accessor( :current_batch )
. . .
def execute
. . .
current_batch = other_class_main_method( x ) # is a hash
. . .
if current_batch
begin
. . .
ao_map_module_method( y )
. . .
end
This construct fails in ao_map_module_method with current_batch being
nil. Why? What is it that I am not comprehending about variable scope
in attempting to do this?
I know that I can pass current_batch in as a method parameter to
ao_map_module_method but what is wrong with simply calling an accessor
method from inside another method? Presumably current_batch and
ao_map_module_method are at the same level inside of class Main.
I clearly have either forgotten something important, or I never
understood it correctly, but I cannot sort out what is wrong with the
following:
You have fallen in the method local variable ambiguity trap.
include AO_Map
. . .
attr_accessor( :current_batch )
. . .
def execute
. . .
current_batch = other_class_main_method( x ) # is a hash
self.current_batch = other_class_main_method( x ) # is a hash
in attempting to do this?
A call to an attribute setter (x=something) must always be prefixed
with a variable and a dot. Otherwise the name is recognized to mean a
local variable and you do not see the getter any more.
I know that I can pass current_batch in as a method parameter to
ao_map_module_method but what is wrong with simply calling an accessor
method from inside another method? Presumably current_batch and
ao_map_module_method are at the same level inside of class Main.
You’re not actually calling the accessor but rather defining a local
variable.