Question on scoping

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.

On Thu, Nov 18, 2010 at 4:21 PM, James B. [email protected]
wrote:

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.

Kind regards

robert

Robert K. wrote in post #962372:

You’re not actually calling the accessor but rather defining a local
variable.

Kind regards

robert

Thanks. As soon as I read your note I had one of the head slapping
moments. I have done this before and, no doubt, will do it again.

On 18.11.2010 21:31, James B. wrote:

Thanks. As soon as I read your note I had one of the head slapping
moments. I have done this before and, no doubt, will do it again.

What do you mean, mixing variables and accessors or slapping your head?
:wink:

Cheers

robert

Robert K. wrote in post #962525:

On 18.11.2010 21:31, James B. wrote:

Thanks. As soon as I read your note I had one of the head slapping
moments. I have done this before and, no doubt, will do it again.

What do you mean, mixing variables and accessors or slapping your head?
:wink:

Cheers

robert

All of the above.