question on instance variable access

S

surfunbear

I came accross a problem which made me wonder. Sort of about dotting
into a variable.
I could find the answer in my ruby book. How would I do the access at
the end here.
I know I could probably use accessors, but is that the only way to do
it ?


class A

def initialize
@num = 3
end

end


class B

def initialize
@friend = A.new
end


myobj = B.new

# syntax here ? Is this allowed, is it done another way ?
x = myobj.@friend.@num
 
D

David Pollak

The code would look like:
myobj.instance_variable_get:)@friend).instance_variable_get:)@num)
 

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,772
Messages
2,569,593
Members
45,104
Latest member
LesliVqm09
Top