From: Grary Stimon on
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/.

From: Robert Klemme on
2010/3/24 Grary Stimon <grary.stimon(a)gmail.com>:
> 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/