Question on scoping

Discussion in 'Ruby' started by James Byrne, Nov 18, 2010.

  1. James Byrne

    James Byrne Guest

    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.

    --
    Posted via http://www.ruby-forum.com/.
    James Byrne, Nov 18, 2010
    #1
    1. Advertising

  2. On Thu, Nov 18, 2010 at 4:21 PM, James Byrne <> 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.

    > 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/
    Robert Klemme, Nov 18, 2010
    #2
    1. Advertising

  3. James Byrne

    James Byrne Guest

    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.

    --
    Posted via http://www.ruby-forum.com/.
    James Byrne, Nov 18, 2010
    #3
  4. On 18.11.2010 21:31, James Byrne 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? ;-)

    Cheers

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Nov 19, 2010
    #4
  5. James Byrne

    James Byrne Guest

    Robert Klemme wrote in post #962525:
    > On 18.11.2010 21:31, James Byrne 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?
    > ;-)
    >
    > Cheers
    >
    > robert


    All of the above.

    --
    Posted via http://www.ruby-forum.com/.
    James Byrne, Nov 21, 2010
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt S.
    Replies:
    0
    Views:
    391
    Matt S.
    May 4, 2004
  2. Alan Little

    Beginner's scoping question

    Alan Little, Nov 10, 2004, in forum: Python
    Replies:
    6
    Views:
    259
    Alan Little
    Nov 12, 2004
  3. It's me

    A scoping question

    It's me, Dec 28, 2004, in forum: Python
    Replies:
    8
    Views:
    324
    Dennis Lee Bieber
    Dec 28, 2004
  4. 2b|!2b==?

    scoping question

    2b|!2b==?, Apr 21, 2007, in forum: C++
    Replies:
    1
    Views:
    282
    Ivan Vecerina
    Apr 21, 2007
  5. Matt Barnicle
    Replies:
    10
    Views:
    626
    Bruno Desthuilliers
    Dec 2, 2007
Loading...

Share This Page