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

**URL:** <https://rubytalk.org/t/how-can-i-copy-an-array-and-its-elements-call-by-value/54793>\
**Category:** ruby-talk\
**Created:** [7 August 2009 15:18 UTC](https://rubytalk.org/t/how-can-i-copy-an-array-and-its-elements-call-by-value/54793 "2009-08-07T15:18:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Janus\_Bor](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@Janus\_Bor](https://rubytalk.org/u/Janus_Bor)\
**Post date:** [7 August 2009 15:18 UTC](https://rubytalk.org/t/how-can-i-copy-an-array-and-its-elements-call-by-value/54793/1 "2009-08-07T15:18:52Z")

</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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Fleck\_Jean-Julien](https://avatars.discourse-cdn.com/v4/letter/f/65b543/32.png) [@Fleck\_Jean-Julien](https://rubytalk.org/u/Fleck_Jean-Julien)\
**Post date:** [7 August 2009 15:24 UTC](https://rubytalk.org/t/how-can-i-copy-an-array-and-its-elements-call-by-value/54793/2 "2009-08-07T15:24:44Z")

</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,

> **···**
>
> --  
> JJ Fleck  
> PCSI1 Lycée Kleber

---

<div class="post-metadata">

**Author:** ![as1](https://avatars.discourse-cdn.com/v4/letter/a/c77e96/32.png) [@as1](https://rubytalk.org/u/as1)\
**Post date:** [7 August 2009 15:25 UTC](https://rubytalk.org/t/how-can-i-copy-an-array-and-its-elements-call-by-value/54793/3 "2009-08-07T15:25:08Z")

</div>

b=a[0..-1]

> **···**
>
> Le Fri, 7 Aug 2009 10:18:52 -0500, Janus Bor \<janus@urban-youth.com\> a écrit :
> 
> > How can I create an independent copy of 'a'?
> 
> --  
> Arnaud

---

<div class="post-metadata">

**Author:** ![sdjkfhwj\_sdlkjfsdkl](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@sdjkfhwj\_sdlkjfsdkl](https://rubytalk.org/u/sdjkfhwj_sdlkjfsdkl)\
**Post date:** [8 August 2009 02:16 UTC](https://rubytalk.org/t/how-can-i-copy-an-array-and-its-elements-call-by-value/54793/4 "2009-08-08T02:16:33Z")

</div>

Discount Ed hardy tshirt ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Ed hardy jean ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Ed hardy shoes ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Ed hardy handbag ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Ed hardy other porduct ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike air jordans ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Air Max 90 Sneakers ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Air Max 91 Supplier ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Air Max 95 Shoes Supplier ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Air Max 97 Trainers ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Air Max 2003 Wholesale ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Air Max 2004 Shoes Wholesale  
([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Air Max 2005 Shop ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Air Max 2006 Shoes Shop ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Air Max 360 Catalogs ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Air Max Ltd Shoes Catalogs ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Air Max Tn Men's Shoes ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Air Max Tn 2 Women's Shoes ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Air Max Tn 3 Customize ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Air Max Tn 4 Shoes Customize  
( [www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Air Max Tn 6 Supply ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Shox NZ Shoes Supply ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Shox OZ Sale ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Shox TL Store ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Shox TL 2 Shoes Store ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Shox TL 3 Distributor ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Shox Bmw Shoes Distributor ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Shox Elite Shoes Manufacturer  
([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Shox Monster Manufacturer ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Shox R4 Running Shoes ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Shox R5 Mens Shoes ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Shox Ride Womens Shoes ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Shox Rival Shoes Wholesaler ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Shox Energia Wholesaler ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Shox LV Sneaker ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Shox Turbo Suppliers ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Shox Classic Shoes Suppliers  
([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Shox Dendara Trainer ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Air Jordan 1 Seller ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Air Jordan 2 Shoes Seller ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Air Jordan 3 Collection ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Air Jordan 4 Shoes Collection  
([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Air Jordan 5 Chaussure Shoes  
([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Air Jordan 6 Catalog ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Air Jordan 7 Shoes Catalog ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Air Jordan 8 Customized ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Air Jordan 9 Shoes Customized  
([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Air Jordan 10 Wholesalers ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Jordan 11 Shoes Wholesalers ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Air Jordan 12 Factory ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Air Jordan 13 Shoes Factory ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Air Jordan 14 Shoes Sell ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Air Jordan 16 Exporter ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Air Jordan 17 Shoes Exporter  
([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Air Jordan 18 Offer ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Air Jordan 19 Shoes Offer ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Air Jordan 20 Manufacture ([www.ebuyings.com](http://www.ebuyings.com))  
Discount Nike Jordan 21 Shoes Manufacture ([www.ebuyings.com](http://www.ebuyings.com))

---

<div class="post-metadata">

**Author:** ![Janus\_Bor](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@Janus\_Bor](https://rubytalk.org/u/Janus_Bor)\
**Post date:** [7 August 2009 15:47 UTC](https://rubytalk.org/t/how-can-i-copy-an-array-and-its-elements-call-by-value/54793/5 "2009-08-07T15:47:54Z")

</div>

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [7 August 2009 16:30 UTC](https://rubytalk.org/t/how-can-i-copy-an-array-and-its-elements-call-by-value/54793/6 "2009-08-07T16:30:09Z")

</div>

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

&nbsp;&nbsp;robert

> **···**
>
> On 07.08.2009 17:47, Janus Bor wrote:
> 
> > Thank you both, that's exactly what I'm looking for!
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)
