------art_3258_30018097.1141809959323
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

> In the last list of arr1, there's no object3. Why? Does Array#push()
> method append the array as a refrence?


Yes, array elements are just references to objects pretty much in the same
way a variable points to an object. In your case, when you push arr2 into
arr1, what you're doing is making the 3rd element of the array be a
reference to the object pointed to by the variable arr2:

irb(main):006:0> arr1 =[]
=> []
irb(main):007:0> arr2 =[]
=> []
irb(main):008:0> arr1 << "object1"
=> ["object1"]
irb(main):009:0> arr1 << "object2"
=> ["object1", "object2"]
irb(main):010:0> arr2 << "object3"
=> ["object3"]
irb(main):011:0> arr1.push(arr2)
=> ["object1", "object2", ["object3"]]

irb(main):015:0> arr2.object_id        # <==== arr2 and arr1[2] point to the
same object
=> 22439224
irb(main):018:0> arr1[2].object_id
=> 22439224


> If so, why it does? And after
> arr2 had been cleared, I still can access to 3rd member of arr1 without
> exceptions or errors.



The call to arr2.clear() clears the contents of the array object referenced
by arr2, which happens to be the same array the 3rd element of arr1 points
to:

irb(main):012:0> arr2.clear
=> []
irb(main):013:0> arr1
=> ["object1", "object2", []]


The 3rd element is still in arr1, but now it just points to an empty array.

Hope this helps,

Martin

------art_3258_30018097.1141809959323--