[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Adding elements of one array to another

Ari Brown

8/29/2007 10:14:00 PM

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



4 Answers

Todd Benson

8/29/2007 10:38:00 PM

0

On 8/29/07, Ari Brown <ari@aribrown.com> 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

Logan Capaldo

8/29/2007 10:47:00 PM

0

On 8/29/07, Todd Benson <caduceass@gmail.com> wrote:
> On 8/29/07, Ari Brown <ari@aribrown.com> 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.
>
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.
> Todd
>
>

Joel VanderWerf

8/29/2007 10:51:00 PM

0

Logan Capaldo wrote:
> On 8/29/07, Todd Benson <caduceass@gmail.com> wrote:
>> On 8/29/07, Ari Brown <ari@aribrown.com> 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.
>>
> 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]


--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Elad Meidar

8/30/2007 8:31:00 AM

0

On Aug 30, 1:50 am, Joel VanderWerf <vj...@path.berkeley.edu> wrote:
> Logan Capaldo wrote:
> > On 8/29/07, Todd Benson <caduce...@gmail.com> wrote:
> >> On 8/29/07, Ari Brown <a...@aribrown.com> 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.
>
> > 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]
>
> --
> 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>