Question on scoping

J

James Byrne

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.
 
R

Robert Klemme

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.
module AO_Map
=A0. =A0. =A0.
=A0def ao_map_module_method( y )
=A0 puts( current_batch.to_yaml )
. =A0. =A0.
end

class Main
. =A0. =A0.
=A0 =A0require =A0file_dir + "/ao_map"
=A0 =A0include AO_Map
. =A0. =A0.
=A0 =A0attr_accessor( :current_batch )
. =A0. =A0.
=A0 =A0def execute
=A0. =A0. =A0.
=A0 =A0current_batch =3D other_class_main_method( x ) # is a hash

self.current_batch =3D other_class_main_method( x ) # is a hash
=A0 =A0. =A0. =A0.
=A0 =A0if current_batch
=A0 =A0begin
=A0 =A0 =A0. =A0. =A0.
=A0 =A0 =A0ao_map_module_method( y )
=A0 =A0 =A0. =A0. =A0.
end

This construct fails in ao_map_module_method with current_batch being
nil. =A0Why? =A0What is it that I am not comprehending about variable sco= pe
in attempting to do this?

A call to an attribute setter (x=3Dsomething) 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? =A0Presumably 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 variab=
le.

Kind regards

robert


--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
J

James Byrne

Robert Klemme 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.
 
R

Robert Klemme

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? ;-)

Cheers

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top