[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

question on instance variable access

surf

7/25/2006 1:52:00 AM

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

2 Answers

Phillip Hutchings

7/25/2006 1:58:00 AM

0

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

Yes. In Ruby all instance variables are private.

--
Phillip Hutchings
http://www.sit...

David Pollak

7/25/2006 3:06:00 AM

0

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


On 7/24/06, surfunbear@yahoo.com <surfunbear@yahoo.com> wrote:
> 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
>
>
>


--
--------
David Pollak's Ruby Playground
http://d...