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 at
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=[000,111,222,33]
=> [0, 111, 222, 33]
a1=a[1]
=> 1
a[1].object_id
=> 345
a1.object_id
=> 345
a1="NEW VALUE"
=> "NEW VALUE"
a1.object_id
=> -606081098 <----------- Has changed !!!
Because you changed the object that a1 points to.
a[1].object_id <-------- Remains the same
=> 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 = [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 returns the object in the 1 position in the array.
a1 = a[1] points 'a1' at that object. a1 is just a pointer, though, to an object.
You can't change what the Array pointed to by 'a' contains by pointing 'a1' at a different object, which is what your assignment of "NEW STRING" to 'a1' did.
If you want to change what is stored in the 1 index position in the Array pointed to by 'a', you need to be operating on that Array.
b = a
b[1] = "another new string"
puts a[1]
another new string
Make sense?
Kirk Haines
···
On Fri, 25 Apr 2008, [UTF-8] Iñaki Baz Castillo wrote: