Question on instance variable access

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

I know I could probably use accessors, but is that the only way to do
it ?

Yes. In Ruby all instance variables are private.

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs