# How to copy arrays?

**URL:** <https://www.ruby-forum.com/t/how-to-copy-arrays/120848>\
**Category:** Ruby\
**Created:** [November 21, 2007, 4:20am UTC](https://www.ruby-forum.com/t/how-to-copy-arrays/120848 "2007-11-21T04:20:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [November 21, 2007, 4:20am UTC](https://www.ruby-forum.com/t/how-to-copy-arrays/120848/1 "2007-11-21T04:20:17Z")

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

```
 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\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [November 21, 2007, 4:27am UTC](https://www.ruby-forum.com/t/how-to-copy-arrays/120848/2 "2007-11-21T04:27:33Z")

</div>

On Nov 21, 2007 2:20 PM, Martin D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

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

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

---

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [November 21, 2007, 4:38am UTC](https://www.ruby-forum.com/t/how-to-copy-arrays/120848/3 "2007-11-21T04:38:38Z")

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

---

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [November 21, 2007, 4:42am UTC](https://www.ruby-forum.com/t/how-to-copy-arrays/120848/4 "2007-11-21T04:42:46Z")

</div>

On Wed, Nov 21, 2007 at 12:38:36PM +0900, Martin D. wrote:

> 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)  
destination\_array.replace @my\_array  
end

> Thank you in advance  
> –Greg

---

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [November 21, 2007, 5:14am UTC](https://www.ruby-forum.com/t/how-to-copy-arrays/120848/5 "2007-11-21T05:14:53Z")

</div>

On Behalf Of Martin D.:

# 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:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [November 21, 2007, 4:45am UTC](https://www.ruby-forum.com/t/how-to-copy-arrays/120848/6 "2007-11-21T04:45:41Z")

</div>

On Nov 21, 2007 2:38 PM, Martin D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/).

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

---

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [November 21, 2007, 8:00am UTC](https://www.ruby-forum.com/t/how-to-copy-arrays/120848/7 "2007-11-21T08:00:52Z")

</div>

On 21.11.2007 04:38, Martin D. 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.

Cheers

robert

---

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [November 21, 2007, 8:02am UTC](https://www.ruby-forum.com/t/how-to-copy-arrays/120848/8 "2007-11-21T08:02:02Z")

</div>

On 21.11.2007 07:58, Robert K. wrote:

> On 21.11.2007 04:38, Martin D. 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.

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

robert
