From: khaines@... Date: 2008-04-25T01:41:31+09:00 Subject: Re: Pointer concept in Ruby ? --574647984-55221027-1209055290=:5646 Content-Type: MULTIPART/MIXED; BOUNDARY="574647984-55221027-1209055290=:5646" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --574647984-55221027-1209055290=:5646 Content-Type: TEXT/PLAIN; charset=UTF-8; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE On Fri, 25 Apr 2008, [UTF-8] I=C3=B1aki Baz Castillo wrote: > Yes, I know that since object_id is the same **until** a write > operation is done. > > >> If you however call a self-modifying method on a1 while it's pointing a= t >> the array string, it'll also change in the array, as it's still pointing= at >> the same object. > > No, that's not true. Demostration: > > array=3D[000,111,222,33] > =3D> [0, 111, 222, 33] > > a1=3Da[1] > =3D> 1 > > a[1].object_id > =3D> 345 > > a1.object_id > =3D> 345 > > a1=3D"NEW VALUE" > =3D> "NEW VALUE" > >> a1.object_id > =3D> -606081098 <----------- Has changed !!! Because you changed the object that a1 points to. > a[1].object_id <-------- Remains the same > =3D> 345 a[1] points to an object. a1 points to an object. They were the same object, until you pointed a1 at a different object. This is the important distinction that you are missing. To recap: a =3D [1,2,3,4] a points to an Array that contains 4 objects, the Fixnums 1, 2, 3, and 4. a[1] calls a method [] on the object pointed to by 'a'. That method=20 returns the object in the 1 position in the array. a1 =3D a[1] points 'a1' at that object. a1 is just a pointer, though, to a= n=20 object. You can't change what the Array pointed to by 'a' contains by pointing=20 'a1' at a different object, which is what your assignment of "NEW STRING"= =20 to 'a1' did. If you want to change what is stored in the 1 index position in the Array= =20 pointed to by 'a', you need to be operating on that Array. b =3D a b[1] =3D "another new string" puts a[1] another new string Make sense? Kirk Haines --574647984-55221027-1209055290=:5646-- --574647984-55221027-1209055290=:5646--