# Adding elements of one array to another

**URL:** <https://www.ruby-forum.com/t/adding-elements-of-one-array-to-another/113337>\
**Category:** Ruby\
**Created:** [August 30, 2007, 12:15am UTC](https://www.ruby-forum.com/t/adding-elements-of-one-array-to-another/113337 "2007-08-30T00:15:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [August 30, 2007, 12:15am UTC](https://www.ruby-forum.com/t/adding-elements-of-one-array-to-another/113337/1 "2007-08-30T00:15:40Z")

</div>

I can’t remember who asked this a while ago, but while I was  
stumbling through my Programming Ruby book, I found a nice method in  
array that will instantly do the trick: Array#concat

a = [1, 2]  
b = [3, 4]  
a.concat(b) #=\> [1, 2, 3, 4]

I can’t remember if someone already posted this solution, but if not,  
here it is.

Ari  
-------------------------------------------|  
Nietzsche is my copilot

---

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [August 30, 2007, 12:38am UTC](https://www.ruby-forum.com/t/adding-elements-of-one-array-to-another/113337/2 "2007-08-30T00:38:47Z")

</div>

On 8/29/07, Ari B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I can’t remember who asked this a while ago, but while I was  
> stumbling through my Programming Ruby book, I found a nice method in  
> array that will instantly do the trick: Array#concat
> 
> a = [1, 2]  
> b = [3, 4]  
> a.concat(b) #=\> [1, 2, 3, 4]

There’s also a+=b. I’m not sure if there’s a difference.

Todd

---

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [August 30, 2007, 12:47am UTC](https://www.ruby-forum.com/t/adding-elements-of-one-array-to-another/113337/3 "2007-08-30T00:47:48Z")

</div>

On 8/29/07, Todd B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> There’s also a+=b. I’m not sure if there’s a difference.

There is. a += b → a = a + b, and a + b will always allocate a new  
array. a.concat(b) will try to use any space already allocated in a,  
and only if there is not enough will it regrow a.

---

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [August 30, 2007, 12:51am UTC](https://www.ruby-forum.com/t/adding-elements-of-one-array-to-another/113337/4 "2007-08-30T00:51:48Z")

</div>

> > array that will instantly do the trick: Array#concat
> > 
> > a = [1, 2]  
> > b = [3, 4]  
> > a.concat(b) #=\> [1, 2, 3, 4]
> 
> There’s also a+=b. I’m not sure if there’s a difference.
> 
> Todd

There’s a considerable performance difference:

$ cat test.rb  
require ‘benchmark’

n = 50\_000  
Benchmark.bm do |x|  
a = [1,2]  
b = [3,4]  
x.report { n.times { a += b } }  
x.report { n.times { a.concat b } }  
end  
$ ruby test.rb  
user system total real  
5.370000 1.260000 6.630000 ( 6.664451)  
0.030000 0.020000 0.050000 ( 0.051235)

As concat acts directly on self, whereas + creates a third object.

HTH,

Felix

---

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [August 30, 2007, 10:35am UTC](https://www.ruby-forum.com/t/adding-elements-of-one-array-to-another/113337/5 "2007-08-30T10:35:40Z")

</div>

On Aug 30, 1:50 am, Joel VanderWerf [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > There’s also a+=b. I’m not sure if there’s a difference.  
> > > =\> [3, 4]  
> > > irb(main):008:0\> c = a  
> > > =\> [1, 2]  
> > > irb(main):009:0\> a.concat(b)  
> > > =\> [1, 2, 3, 4]  
> > > irb(main):010:0\> c  
> > > =\> [1, 2, 3, 4]
> 
> –  
> vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

You can also use OR to uniquely concat those arrays.  
irb(main):003:0\> [1,2] | [3,4]  
=\> [1, 2, 3, 4]  
irb(main):004:0\> [1,2] | [2,3,4]  
=\> [1, 2, 3, 4]  
irb(main):005:0\>

---

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [August 30, 2007, 12:52am UTC](https://www.ruby-forum.com/t/adding-elements-of-one-array-to-another/113337/6 "2007-08-30T00:52:48Z")

</div>

Logan C. wrote:

> > There’s also a+=b. I’m not sure if there’s a difference.
> 
> There is. a += b --\> a = a + b, and a + b will always allocate a new  
> array. a.concat(b) will try to use any space already allocated in a,  
> and only if there is not enough will it regrow a.

And note that concat has side effects:

irb(main):001:0\> a = [1,2]  
=\> [1, 2]  
irb(main):002:0\> b = [3,4]  
=\> [3, 4]  
irb(main):003:0\> c = a  
=\> [1, 2]  
irb(main):004:0\> a += b  
=\> [1, 2, 3, 4]  
irb(main):005:0\> c  
=\> [1, 2]  
irb(main):006:0\> a = [1,2]  
=\> [1, 2]  
irb(main):007:0\> b = [3,4]  
=\> [3, 4]  
irb(main):008:0\> c = a  
=\> [1, 2]  
irb(main):009:0\> a.concat(b)  
=\> [1, 2, 3, 4]  
irb(main):010:0\> c  
=\> [1, 2, 3, 4]
