# How to copy arrays?

**URL:** <https://rubytalk.org/t/how-to-copy-arrays/42387>\
**Category:** ruby-talk\
**Created:** [21 November 2007 03:20 UTC](https://rubytalk.org/t/how-to-copy-arrays/42387 "2007-11-21T03:20:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_Durai](https://avatars.discourse-cdn.com/v4/letter/m/d2c977/32.png) [@Martin\_Durai](https://rubytalk.org/u/Martin_Durai)\
**Post date:** [21 November 2007 03:20 UTC](https://rubytalk.org/t/how-to-copy-arrays/42387/1 "2007-11-21T03:20:15Z")

</div>

I came accross another problem, how to copy arrays? Actually i need to  
copy array just like in java. i will include the source code of java.  
The same functionality i need in ruby. please any body help me with this

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.arraycopy(elName, 0, arr, 0, elStackSize);

I need a way to do samething in ruby. please any body help me with code  
for arraycopy.

thanks in advance

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

---

<div class="post-metadata">

**Author:** ![Daniel\_N1](https://avatars.discourse-cdn.com/v4/letter/d/df788c/32.png) [@Daniel\_N1](https://rubytalk.org/u/Daniel_N1)\
**Post date:** [21 November 2007 03:26 UTC](https://rubytalk.org/t/how-to-copy-arrays/42387/2 "2007-11-21T03:26:49Z")

</div>

The java doesn't mean anything to me 🙂 That make me very happy.

@array.dup is the way to copy any object in ruby

> **···**
>
> On Nov 21, 2007 2:20 PM, Martin Durai \<martin@angleritech.com\> wrote:
> 
> > I came accross another problem, how to copy arrays? Actually i need to  
> > copy array just like in java. i will include the source code of java.  
> > The same functionality i need in ruby. please any body help me with this
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;System.arraycopy(elName, 0, arr, 0, elStackSize);
> > 
> > I need a way to do samething in ruby. please any body help me with code  
> > for arraycopy.
> > 
> > thanks in advance

---

<div class="post-metadata">

**Author:** ![Martin\_Durai](https://avatars.discourse-cdn.com/v4/letter/m/d2c977/32.png) [@Martin\_Durai](https://rubytalk.org/u/Martin_Durai)\
**Post date:** [21 November 2007 03:38 UTC](https://rubytalk.org/t/how-to-copy-arrays/42387/3 "2007-11-21T03:38:36Z")

</div>

Thank you daniel, but my functionality is not return as a array.  
I need to copy a source array to a destination array.

Thank you in advance

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

---

<div class="post-metadata">

**Author:** ![Gregory\_Seidman](https://avatars.discourse-cdn.com/v4/letter/g/eb9ed0/32.png) [@Gregory\_Seidman](https://rubytalk.org/u/Gregory_Seidman)\
**Post date:** [21 November 2007 03:42 UTC](https://rubytalk.org/t/how-to-copy-arrays/42387/4 "2007-11-21T03:42:01Z")

</div>

> Thank you daniel, but my functionality is not return as a array.  
> I need to copy a source array to a destination array.

def java\_inspired\_method\_sans\_idiomatic\_ruby(destination\_array)  
&nbsp;&nbsp;destination\_array.replace @my\_array  
end

> Thank you in advance

--Greg

> **···**
>
> On Wed, Nov 21, 2007 at 12:38:36PM +0900, Martin Durai wrote:

---

<div class="post-metadata">

**Author:** ![Daniel\_N1](https://avatars.discourse-cdn.com/v4/letter/d/df788c/32.png) [@Daniel\_N1](https://rubytalk.org/u/Daniel_N1)\
**Post date:** [21 November 2007 03:44 UTC](https://rubytalk.org/t/how-to-copy-arrays/42387/5 "2007-11-21T03:44:51Z")

</div>

I don't understand what your asking then. This is a fairly standard way to  
copy an object.  
@array = [1,2,3,4,5]

@new\_array = @array.dup

> **···**
>
> On Nov 21, 2007 2:38 PM, Martin Durai \<martin@angleritech.com\> wrote:
> 
> > Thank you daniel, but my functionality is not return as a array.  
> > I need to copy a source array to a destination array.
> > 
> > Thank you in advance  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [21 November 2007 04:14 UTC](https://rubytalk.org/t/how-to-copy-arrays/42387/6 "2007-11-21T04:14:16Z")

</div>

On Behalf Of Martin Durai:  
# Thank you daniel, but my functionality is not return as a array.  
# I need to copy a source array to a destination array.

i think you only want to copy a part (the whole is trivial) of the array and insert it to another array, no?

a  
#=\> [1, 2, 3, 4]

b  
#=\> [5, 6, 7, 8, 9, 10]

a[1,1]  
#=\> [2]

b[2..-2]  
#=\> [7, 8, 9]

a[1,1]=b[2..-2]  
#=\> [7, 8, 9]

a  
#=\> [1, 7, 8, 9, 3, 4]

or maybe just plain insert it,

a[0]  
#=\> 1

b[0,2]  
#=\> [5, 6]

a[0]=b[0,2]  
#=\> [5, 6]

a  
#=\> [[5, 6], 7, 8, 9, 3, 4]

kind regards -botp

---

<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:** [21 November 2007 07:00 UTC](https://rubytalk.org/t/how-to-copy-arrays/42387/7 "2007-11-21T07:00:01Z")

</div>

Maybe before we go off guessing you can show a bit more of your code. While there are methods to copy and insert into another array, maybe there is a more efficient solution.

Cheers

&nbsp;&nbsp;robert

> **···**
>
> On 21.11.2007 04:38, Martin Durai wrote:
> 
> > Thank you daniel, but my functionality is not return as a array.  
> > I need to copy a source array to a destination array.

---

<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:** [21 November 2007 07:00 UTC](https://rubytalk.org/t/how-to-copy-arrays/42387/8 "2007-11-21T07:00:02Z")

</div>

I meant to include this

irb(main):007:0\> a=(1..10).to\_a  
=\> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]  
irb(main):008:0\> a[2,4]=%w{a b c}  
=\> ["a", "b", "c"]  
irb(main):009:0\> a  
=\> [1, 2, "a", "b", "c", 7, 8, 9, 10]  
irb(main):010:0\> a.concat %w{foo bar}  
=\> [1, 2, "a", "b", "c", 7, 8, 9, 10, "foo", "bar"]  
irb(main):011:0\> a  
=\> [1, 2, "a", "b", "c", 7, 8, 9, 10, "foo", "bar"]  
irb(main):012:0\>

&nbsp;&nbsp;robert

> **···**
>
> On 21.11.2007 07:58, Robert Klemme wrote:
> 
> > On 21.11.2007 04:38, Martin Durai wrote:
> > 
> > > Thank you daniel, but my functionality is not return as a array.  
> > > I need to copy a source array to a destination array.
> > 
> > Maybe before we go off guessing you can show a bit more of your code. While there are methods to copy and insert into another array, maybe there is a more efficient solution.
