class instance variable visibility to nested modules...

Discussion in 'Ruby' started by Grary Stimon, Mar 24, 2010.

  1. Grary Stimon

    Grary Stimon Guest

    Hi,

    I'd like to organize the inner namespace of a class s.t. an inner, or
    'nested', module (or class) has access to the instance variables of the
    outer class. After experimenting a bit, it seems there is no visibility
    to the outer class's instance variables possible. Do I conclude
    correctly?

    Thanks,

    Grar
    --
    Posted via http://www.ruby-forum.com/.
     
    Grary Stimon, Mar 24, 2010
    #1
    1. Advertising

  2. 2010/3/24 Grary Stimon <>:
    > I'd like to organize the inner namespace of a class s.t. an inner, or
    > 'nested', module (or class) has access to the instance variables of the
    > outer class. After experimenting a bit, it seems there is no visibility
    > to the outer class's instance variables possible. Do I conclude
    > correctly?


    Yes and no. If you use constants, then you can access them. Even
    instance variables can be accessed via instance_variable_get

    irb(main):001:0> class X
    irb(main):002:1> @foo = 123
    irb(main):003:1> FOO = 456
    irb(main):004:1>
    irb(main):005:1* class Y
    irb(main):006:2> p @foo, X.instance_variable_get('@foo'), FOO
    irb(main):007:2> end
    irb(main):008:1> end
    nil
    123
    456
    => [nil, 123, 456]
    irb(main):009:0>

    Note: I assume you are talking about instance variables of the class
    instance. In Ruby there is no class nesting as in Java where non
    static inner classes have access to an instance of the outer class.

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Mar 24, 2010
    #2
    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. E11
    Replies:
    1
    Views:
    4,901
    Thomas Weidenfeller
    Oct 12, 2005
  2. Sridhar R
    Replies:
    14
    Views:
    1,446
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    Feb 10, 2004
  3. Gerry Sutton
    Replies:
    1
    Views:
    567
    Peter Otten
    Apr 16, 2005
  4. Martin P. Hellwig
    Replies:
    1
    Views:
    384
    Martin P. Hellwig
    Mar 26, 2010
  5. David Garamond
    Replies:
    5
    Views:
    271
    Ara.T.Howard
    Jun 8, 2004
Loading...

Share This Page