Discussing the various options to add two or more arrays in Ruby
Introduction
- In this article, we will discuss 9 options to add two arrays in Ruby.
- We will also discuss how we can add more than two arrays in Ruby.
Adding 2 Arrays
Option 1
- We can use the contact method on an array to add another array to it.
irb(main):001:0> a1 = [1,2,3]
=> [1, 2, 3]
irb(main):002:0> a2 = [4,5,6]
=> [4, 5, 6]
irb(main):003:0> a1.concat a2
=> [1, 2, 3, 4, 5, 6]
Option 2
- We can also use the + operator to add two arrays.
irb(main):001:0> a1 = [1,2,3]
=> [1, 2, 3]
irb(main):002:0> a2 = [4,5,6]
=> [4, 5, 6]
irb(main):004:0> a1 + a2
=> [1, 2, 3, 4, 5, 6, 4, 5, 6]
Option 3
- If we use the shovel operator it will add the entire as a single element in the first array.
- And then we can use flatten to flatten the elements in the result array.
irb(main):001:0> a1 = [1,2,3]
=> [1, 2, 3]
irb(main):002:0> a2 = [4,5,6]
=> [4, 5, 6]
irb(main):006:0> a1 << a2
=> [1, 2, 3, 4, 5, 6, [4, 5, 6]]
irb(main):007:0> (a1 << a2).flatten
=> [1, 2, 3, 4, 5, 6, 4, 5, 6, 4, 5, 6]
Option 4
- We can use the push method on the array to add an array as an element.
- We can use flatten to flatten the elements on the result array.
irb(main):001:0> a1 = [1,2,3]
=> [1, 2, 3]
irb(main):002:0> a2 = [4,5,6]
=> [4, 5, 6]
irb(main):017:0> a1.push(a2)
=> [1, 2, 3, [4, 5, 6]]
irb(main):018:0> a1.push(a2).flatten
=> [1, 2, 3, 4, 5, 6, 4, 5, 6]
Option 5
- We can also split the operator on the second array to destructure it and push them to the first array.
irb(main):001:0> a1 = [1,2,3]
=> [1, 2, 3]
irb(main):002:0> a2 = [4,5,6]
=> [4, 5, 6]
irb(main):021:0> a1.push(*a2)
=> [1, 2, 3, 4, 5, 6]
Option 6
- Another option is to use the splat operator on both the array and add the elements to another array.
irb(main):001:0> a1 = [1,2,3]
=> [1, 2, 3]
irb(main):002:0> a2 = [4,5,6]
=> [4, 5, 6]
irb(main):027:0> [*a1, *a2]
=> [1, 2, 3, 4, 5, 6, 4, 5, 6]
Adding N Arrays
Option 1
- If we have an array of arrays we can simply use flatten to flatten its elements.
[[1,2,3],[4,5,6],[7,8,9]].flatten
Option 2
- Another option is that we can iterate over each of them and add using the + operator.
irb(main):001:0> a1 = [1,2,3]
=> [1, 2, 3]
irb(main):002:0> a2 = [4,5,6]
=> [4, 5, 6]
irb(main):087:0> [[1,2,3],[4,5,6],[7,8,9]].each.reduce(:+)
=> [1, 2, 3, 4, 5, 6, 7, 8, 9]
Option 3
- We can define the utility method add_n_arr method that takes n number of arrays.
- Using the splat operator we can construct an array that has each element array as a nested array.
def add_n_arr(*arr)
p arr
end
add_n_arr([1,2,3], [4,5,6], [6,5,6])
# [[1, 2, 3], [4, 5, 6], [6, 5, 6]]
- We can flatten the arr to return a single concatenated array.
def add_n_arr(*arr)
p arr.flatten
end
add_n_arr([1,2,3], [4,5,6], [6,5,6])
# [1, 2, 3, 4, 5, 6, 6, 5, 6]
Conclusion
- In this article, we have discussed various options to add two or more arrays.
- Ruby provides a very easy-to-use API to add multiple arrays.
Before You Leave
- If you want to upskill your Ruby skills, you should definitely check out the bestseller course Learn to Code with Ruby ( 75k students enrolled, 4.7 stars )