class instance variable visibility to nested modules...

G

Grary Stimon

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
 
R

Robert Klemme

2010/3/24 Grary Stimon said:
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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top