Ruby Joins multiple arrays

Introduction

Arrays can have different arrays on their elements, so

[1,2,3,[4,5,6],7,8,9]
And
[[1,2,3],[4,5,6],[7,8,9]]

You can create an array that looks like this. There are many methods and operators for joining and adding arrays when originally separate arrays are combined into one array as described above.

However, I was confused when I used it in various ways. Moreover, it seems that the usage is slightly different, so I tried some, so I will summarize it here.

1. push method

The push method adds the value specified as one of the elements to the end of the array.

a = [1,2,3]
b = [4,5,6],[7,8,9]
c = [10,11,12],[13,14,15]

a.push(b)
=> [1, 2, 3, [[4, 5, 6], [7, 8, 9]]]
b.push(a)
=> [[4, 5, 6], [7, 8, 9], [1, 2, 3]]
b.push(c)
=> [[4, 5, 6], [7, 8, 9], [[10, 11, 12], [13, 14, 15]]]

Even if there are multiple arrays to add, it seems to be ** element ** of the receiver array. So b = [4,5,6], [7,8,9] is added to the end as [[4,5,6], [7,8,9]].

Also, the push method is a destructive method. The value of the receiver changes.

a.push(b)
=> [1, 2, 3, [[4, 5, 6], [7, 8, 9]]]

a
=> [1, 2, 3, [[4, 5, 6], [7, 8, 9]]]

2. << operator

The << operator seems to behave the same as the push method.

a = [1,2,3]
b = [4,5,6],[7,8,9]
c = [10,11,12],[13,14,15]

a << b
=> [1, 2, 3, [[4, 5, 6], [7, 8, 9]]]
b << a
=> [[4, 5, 6], [7, 8, 9], [1, 2, 3]]
b << c
=> [[4, 5, 6], [7, 8, 9], [[10, 11, 12], [13, 14, 15]]]

it's the same! It's also a destructive method.

a << b
=> [1, 2, 3, [[4, 5, 6], [7, 8, 9]]]

a
=> [1, 2, 3, [[4, 5, 6], [7, 8, 9]]]

3. + operator

The + operator adds the elements of the array to be added one by one.

a = [1,2,3]
b = [4,5,6],[7,8,9]
c = [10,11,12],[13,14,15]
d = [-1,-2,-3]

a + b
=> [1, 2, 3, [4, 5, 6], [7, 8, 9]]
b + a
=> [[4, 5, 6], [7, 8, 9], 1, 2, 3]
b + c
=> [[4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15]]
a + d
=> [1, 2, 3, -1, -2, -3]

In the array of ʻa, each element is a numerical value, so the value is added to the end. In the b` array, each element is an array, so it will be added to the end as it is.

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

If you want to

[a] + b
=> [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

It seems that ʻa` itself should be given as an element of the array.

The + operator makes no changes to the value.

a + b
=> [1, 2, 3, [4, 5, 6], [7, 8, 9]]

a
=> [1, 2, 3]

4. concat method

The concat method behaves much like the+operator.

a = [1,2,3]
b = [4,5,6],[7,8,9]
c = [10,11,12],[13,14,15]
d = [-1,-2,-3]

a.concat(b)
=> [1, 2, 3, [4, 5, 6], [7, 8, 9]]
b.concat(a)
=> [[4, 5, 6], [7, 8, 9], 1, 2, 3]
b.concat(c)
=> [[4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15]]
a.concat(d)
=> [1, 2, 3, -1, -2, -3]

Is the same.

[a].concat(b)
=> [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

The method of treating it as an element of an array is also effective here.

However, the concat method is destructive.

a.concat(b)
=> [1, 2, 3, [4, 5, 6], [7, 8, 9]]

a
=> [1, 2, 3, [4, 5, 6], [7, 8, 9]]

This is the difference from the + operator.

Bonus. ʻunshift` method

The ʻunshift method is very similar to the push` method.

--Add the push method to ** at the end **. --ʻUnshift` method is added to ** beginning **.

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

If you want to make this shape, I posted the method with the + operator etc., but you can also realize it by using the ʻunshift` method.

a = [1,2,3]
b = [4,5,6],[7,8,9]

b.unshift(a)
=> [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

at the end

I couldn't find an article that summarizes how to combine arrays as arrays, so I tried to summarize them myself. Please let me know if you have any mistakes.

Recommended Posts

Ruby Joins multiple arrays
Ruby Learning # 13 Arrays
About Ruby arrays
Basic methods of Ruby arrays
[Ruby] Find numbers in arrays
[Ruby] Escape from multiple loops [Nest]
[Beginner] Various iterative processing for Ruby arrays