# How can I copy an array and its elements? (call by value)

**URL:** <https://www.ruby-forum.com/t/how-can-i-copy-an-array-and-its-elements-call-by-value/173379>\
**Category:** Ruby\
**Created:** [August 7, 2009, 5:18pm UTC](https://www.ruby-forum.com/t/how-can-i-copy-an-array-and-its-elements-call-by-value/173379 "2009-08-07T17:18:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![janus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c2a13f/32.png) [@janus](https://www.ruby-forum.com/u/janus)\
**Post date:** [August 7, 2009, 5:18pm UTC](https://www.ruby-forum.com/t/how-can-i-copy-an-array-and-its-elements-call-by-value/173379/1 "2009-08-07T17:18:50Z")

</div>

Hi all,

I’m failing at something really basic: copying an array and its  
contents, so that I can modify the copy without modifying the original.

Here’s an example:

irb(main):006:0\> a = [1, 2]  
=\> [1, 2]  
irb(main):007:0\> b = a  
=\> [1, 2]  
irb(main):008:0\> b.delete(1)  
=\> 1  
irb(main):009:0\> b  
=\> [2]  
irb(main):010:0\> a  
=\> [2]

I guess the problem is that arrays are called by name not by value, so  
that ‘b’ copy points to the same adress in memory as ‘a’. How can I  
create an independent copy of ‘a’?

Thanks,  
Janus

---

<div class="post-metadata">

**Author:** ![janus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c2a13f/32.png) [@janus](https://www.ruby-forum.com/u/janus)\
**Post date:** [August 7, 2009, 5:25pm UTC](https://www.ruby-forum.com/t/how-can-i-copy-an-array-and-its-elements-call-by-value/173379/2 "2009-08-07T17:25:40Z")

</div>

Hello,

> I’m failing at something really basic: copying an array and its  
> contents, so that I can modify the copy without modifying the original.

I think the method ‘dup’ is what you are looking for

irb(main):001:0\> a = [1,2]  
=\> [1, 2]  
irb(main):002:0\> b = a.dup  
=\> [1, 2]  
irb(main):003:0\> b.delete(1)  
=\> 1  
irb(main):004:0\> b  
=\> [2]  
irb(main):005:0\> a  
=\> [1, 2]

Cheers,

---

<div class="post-metadata">

**Author:** ![janus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c2a13f/32.png) [@janus](https://www.ruby-forum.com/u/janus)\
**Post date:** [August 7, 2009, 5:26pm UTC](https://www.ruby-forum.com/t/how-can-i-copy-an-array-and-its-elements-call-by-value/173379/3 "2009-08-07T17:26:15Z")

</div>

Le Fri, 7 Aug 2009 10:18:52 -0500,  
Janus B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) a Ã©crit :

> How can I create an independent copy of ‘a’?

b=a[0…-1]

---

<div class="post-metadata">

**Author:** ![janus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c2a13f/32.png) [@janus](https://www.ruby-forum.com/u/janus)\
**Post date:** [August 7, 2009, 5:47pm UTC](https://www.ruby-forum.com/t/how-can-i-copy-an-array-and-its-elements-call-by-value/173379/4 "2009-08-07T17:47:51Z")

</div>

Thank you both, that’s exactly what I’m looking for!

---

<div class="post-metadata">

**Author:** ![janus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c2a13f/32.png) [@janus](https://www.ruby-forum.com/u/janus)\
**Post date:** [August 7, 2009, 6:33pm UTC](https://www.ruby-forum.com/t/how-can-i-copy-an-array-and-its-elements-call-by-value/173379/5 "2009-08-07T18:33:35Z")

</div>

On 07.08.2009 17:47, Janus B. wrote:

> Thank you both, that’s exactly what I’m looking for!

Just note that #dup and #clone do a shallow copy, so if you modify  
objects in the copy this change will also be seen via the original  
Array. Depending on what you do #map might be more appropriate.

Kind regards

robert
