From: bbiker Date: 2006-07-09T00:15:06+09:00 Subject: Re: storing and using references to variables Russell Fulton wrote: > Hi, > I want to store a reference to a scalar variable (in perl parlance) > in another variable and then assign a value to the stored variable. I > have done this for contiainers by simply assigning the container to the > variable: > > ref = a = [] > > ref[0] = 'x' > > leaves a[0] == 'x' > > as it should. > > I want to be able to do something similar but using a simple variable > instead of an array. E.g. in perl > > $ref = \$a > > $$ref = 'x' > > but I can not find anything about explicitly dereferencing a variable. > > what I have done in the mean time is to use an array and only use the > first element. > In Ruby, all variables (except for number objects) are references and there is no need to implicitly derefence. now all methods that do not involve an assignment on one will be reflected will be reflected on the other Try the following in irb astring = "this is a string object" bstring = astring or bstring = astring = "" or bstring = astring = String.new astring << "this is a string object" astring.object_id bstring.object_id will show that both variables point to the same object now all methods invoked that do not involve an assignment on one will be reflected on the other for example: bstring.capitalize! # both bstring and astring are identical if you bstring = "this is a new string object" bstring.object_id shows that bstring points to a different object and astring and bstring are no longer the same the same would have happened if you ref = [ 1, 2, 3 ] now ref and a are different objects