# Array#replace vs assignment

**URL:** <https://www.ruby-forum.com/t/array-replace-vs-assignment/62857>\
**Category:** Ruby\
**Created:** [June 8, 2006, 7:55pm UTC](https://www.ruby-forum.com/t/array-replace-vs-assignment/62857 "2006-06-08T19:55:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![toadkicker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@toadkicker](https://www.ruby-forum.com/u/toadkicker)\
**Post date:** [June 8, 2006, 7:55pm UTC](https://www.ruby-forum.com/t/array-replace-vs-assignment/62857/1 "2006-06-08T19:55:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![toadkicker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@toadkicker](https://www.ruby-forum.com/u/toadkicker)\
**Post date:** [June 8, 2006, 7:58pm UTC](https://www.ruby-forum.com/t/array-replace-vs-assignment/62857/2 "2006-06-08T19:58:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![toadkicker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@toadkicker](https://www.ruby-forum.com/u/toadkicker)\
**Post date:** [June 8, 2006, 8:27pm UTC](https://www.ruby-forum.com/t/array-replace-vs-assignment/62857/3 "2006-06-08T20:27:06Z")

</div>

On 6/8/06, Marcel Molina Jr. [removed\_email\_address@domain.invalid](mailto: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.

---

<div class="post-metadata">

**Author:** ![toadkicker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@toadkicker](https://www.ruby-forum.com/u/toadkicker)\
**Post date:** [June 8, 2006, 8:52pm UTC](https://www.ruby-forum.com/t/array-replace-vs-assignment/62857/4 "2006-06-08T20:52:17Z")

</div>

On 6/8/06, Jacob F. [removed\_email\_address@domain.invalid](mailto: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
