From: Brian Ross
Date: 2008-08-15T11:38:42+09:00
Subject: Re: Use of dup to remove references
------=_Part_35174_2781342.1218768092465
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
On Thu, Aug 14, 2008 at 10:20 PM, botp wrote:
> variables are just references to objects. when you do
>
> y = "test"
> x = y
>
> x and y now references to the original string object "test"
>
Thanks so much for the help. What has been confusing is that the object
changes in this next bit of test code so that both variables reference
different objects:
# Start of code that changes object
sentence = "first"
p sentence.object_id
sentence_new = sentence
p sentence_new.object_id
sentence = "second"
p sentence.object_id
p sentence_new.object_id
# End of code
and I think that I figured out that the [] method doesn't change the object
referenced, but just the value of the object, while using = to assign a new
value assigns a new object as well. Is that correct? Is there a way to use =
without changing the object in the example above?
# Start of code that preserves object
sentence = "test"
p sentence.object_id
sentence_new = sentence
p sentence_new.object_id
sentence['t'] = 'p'
p sentence.object_id
puts sentence_new
puts sentence
# End of code
Thanks again
Brian
------=_Part_35174_2781342.1218768092465--