Array#replace vs assignment


#1

Is there any difference between the following:

a = [‘a’,‘b’,‘c’,‘d’,‘e’]
z = [‘x’,‘y’,‘z’]
a.replace(z)

and

a = [‘a’,‘b’,‘c’,‘d’,‘e’]
z = [‘x’,‘y’,‘z’]
a = z

They seem to do the same thing.

Thanks!

Todd B.


#2

On Fri, Jun 09, 2006 at 02:54:32AM +0900, Todd B. wrote:

Is there any difference between the following:

a = [‘a’,‘b’,‘c’,‘d’,‘e’]
z = [‘x’,‘y’,‘z’]
a.replace(z)

a.object_id == z.object_id

=> false

and

a = [‘a’,‘b’,‘c’,‘d’,‘e’]
z = [‘x’,‘y’,‘z’]
a = z

a.object_id == z.object_id

=> true

marcel


#3

On 6/8/06, Marcel Molina Jr. removed_email_address@domain.invalid wrote:

and

a = [‘a’,‘b’,‘c’,‘d’,‘e’]
z = [‘x’,‘y’,‘z’]
a = z

a.object_id == z.object_id

=> true

Also:

a = [‘a’,‘b’,‘c’,‘d’,‘e’]
b = a
z = [‘x’,‘y’,‘z’]
a.replace(z)
p b # => [‘x’, ‘y’, ‘z’]

vs.

a = [‘a’,‘b’,‘c’,‘d’,‘e’]
b = a
z = [‘x’,‘y’,‘z’]
a = z
p b # => [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

Jacob F.


#4

On 6/8/06, Jacob F. removed_email_address@domain.invalid wrote:

z = [‘x’,‘y’,‘z’]
a = z
p b # => [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

Jacob F.

OK. I get it. When I use the assignment, a becomes (points to the same
object as) z. Using replace, both maintain their individuality.

Thanks for the clarification.

Todd