From: "Wayne E. Seguin" Date: 2007-09-29T20:46:01+09:00 Subject: Re: Noobie question: saving the values ------=_Part_1639_31178456.1191066361272 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Content-Disposition: inline On 9/29/07, Druhie Almighty wrote: > > So can you please explain me why do these two operations very close to > each other result in different ways, considering the fact that both a > and b contain the same link? > > 1) > a="13" > b=a > a="12" > puts b --> 13 > > 2) > a="13" > b=a > a[1]="2" > puts b --> 12 You're first way makes 'a' point at a new string object "12". It appears that you are thinking of C strings :) Strings in Ruby are a bit different. You can access individual characters using the [] operator. Indexes are 0 based so when you did a[1] where a was a string, you were assigning the number '2' to the second position in the string, hence overwriting the '3'. What you were probably intending to do was something like this: >> a = [ "13" ] => ["13"] >> b = a => ["13"] >> a[1] = 2 => 2 >> a => ["13", 2] Also note that the reason this change reflected when you printed out 'b' is because after the assignment "b=a", 'b' will refer to the variable 'a'. Meaning it does not create a new object but instead becomes a name for the old so that both 'a' and 'b' are both names for the same object. Other ways you can append elements to the end of the array: >> a << "3" => ["13", 2, "3"] >> a.push "4" => ["13", 2, "3", "4"] You can checkout the Array methods available to you here: http://www.ruby-doc.org/core/classes/Array.html I hope this helps, ~Wayne ------=_Part_1639_31178456.1191066361272--