OneBite.Dev - Coding blog in a bite size

merge two arrays in Ruby

Code snippet on how to merge two arrays in Ruby

  array1 = [1, 2, 3]
  array2 = [4, 5, 6]
  array3 = array1 + array2

This code allows us to merge two arrays. First, it creates two arrays, array1 and array2, both containing three elements. Then, it creates a new array, array3, which is the combination of the elements in array1 and array2. More specifically, array3 is the sum of array1 and array2. In other words, this code takes the elements from array1 and array2 and adds them together to create a single array, array3.

ruby