Ruby offers several ways to combine arrays, each with its own characteristics. This guide explores the most common and efficient methods, providing examples to illustrate their behavior.
Table of Contents
- Merging Arrays with
concat
- Merging Arrays with the
+
Operator - Merging Arrays with
push
and the Splat Operator - Merging Arrays with
append
- Merging Arrays with
union
- Merging Arrays with the
|
Operator - Conclusion
1. Merging Arrays with concat
The concat
method modifies the original array by adding the elements of another array to its end. It’s an in-place operation, directly altering the array it’s called upon.
array1 = [1, 2, 3]
array2 = [4, 5, 6]
array1.concat(array2)
puts array1 # Output: [1, 2, 3, 4, 5, 6]
puts array2 # Output: [4, 5, 6] (array2 remains unchanged)
2. Merging Arrays with the +
Operator
The +
operator creates a new array containing all elements from both arrays. The original arrays remain untouched.
array1 = [1, 2, 3]
array2 = [4, 5, 6]
merged_array = array1 + array2
puts merged_array # Output: [1, 2, 3, 4, 5, 6]
puts array1 # Output: [1, 2, 3] (array1 remains unchanged)
puts array2 # Output: [4, 5, 6] (array2 remains unchanged)
3. Merging Arrays with push
and the Splat Operator
The push
method adds elements to the end of an array. The splat operator (*
) unpacks another array, allowing you to add all its elements at once. This, like concat
, modifies the original array.
array1 = [1, 2, 3]
array2 = [4, 5, 6]
array1.push(*array2)
puts array1 # Output: [1, 2, 3, 4, 5, 6]
puts array2 # Output: [4, 5, 6] (array2 remains unchanged)
4. Merging Arrays with append
The append
method is a synonym for push
; they function identically.
array1 = [1, 2, 3]
array2 = [4, 5, 6]
array1.append(*array2)
puts array1 # Output: [1, 2, 3, 4, 5, 6]
puts array2 # Output: [4, 5, 6] (array2 remains unchanged)
5. Merging Arrays with union
The union
method (or its alias, the |
operator) returns a new array containing only the unique elements from both input arrays. Duplicate elements are eliminated.
array1 = [1, 2, 3, 3]
array2 = [3, 4, 5]
merged_array = array1.union(array2)
puts merged_array # Output: [1, 2, 3, 4, 5]
puts array1 # Output: [1, 2, 3, 3] (array1 remains unchanged)
puts array2 # Output: [3, 4, 5] (array2 remains unchanged)
6. Merging Arrays with the |
Operator
This is shorthand for the union
method, producing the same result.
array1 = [1, 2, 3, 3]
array2 = [3, 4, 5]
merged_array = array1 | array2
puts merged_array # Output: [1, 2, 3, 4, 5]
puts array1 # Output: [1, 2, 3, 3] (array1 remains unchanged)
puts array2 # Output: [3, 4, 5] (array2 remains unchanged)
7. Conclusion
Ruby provides diverse methods for array merging, allowing you to select the optimal approach based on your specific needs. Consider whether in-place modification or a new array is required and whether duplicate elements should be preserved. For creating new arrays, the +
operator and union
method generally offer superior readability. concat
and push
are efficient for modifying arrays directly. Understanding these nuances ensures writing clear and efficient Ruby code.