Array.concat / + / flatten / zip
Methods for combining multiple arrays or pairing elements across them. You can perform set operations using operators (+, -, &, |), or use zip to match elements by index.
Syntax
# Returns a new array that concatenates the given arrays. array.concat(other_array, ...) array + other_array # Set operations (difference, intersection, union) array - other_array # Difference: elements in left but not in right array & other_array # Intersection: elements common to both array | other_array # Union: all elements from either (no duplicates) # Flattens a nested array. array.flatten array.flatten(depth) # Returns an array pairing elements from multiple arrays by index. array.zip(other_array, ...)
Method List
| Method / Operator | Description |
|---|---|
| concat(arr, ...) | Appends elements from the given arrays to the end of the array (mutates the original). Multiple arrays can be specified at once. |
| array + other | Returns a new array that combines both arrays (non-destructive). |
| array - other | Returns the difference: a new array with all elements of the right array removed from the left. |
| array & other | Returns the intersection: a new array containing only elements common to both arrays (no duplicates). |
| array | other | Returns the union: a new array containing all elements from either array (no duplicates). |
| flatten(depth) | Returns a new flattened array. If a depth is given, flattens only that many levels deep. |
| zip(arr, ...) | Returns an array of arrays, pairing elements at the same index from each array. |
Sample Code
# Concatenating arrays
a = [1, 2, 3]
b = [4, 5, 6]
puts (a + b).inspect # [1, 2, 3, 4, 5, 6]
c = [1, 2, 3]
c.concat([4, 5], [6, 7]) # Mutates: appends to the original array
puts c.inspect # [1, 2, 3, 4, 5, 6, 7]
# Set operations
x = [1, 2, 3, 4, 5]
y = [3, 4, 5, 6, 7]
puts (x - y).inspect # [1, 2] (difference)
puts (x & y).inspect # [3, 4, 5] (intersection)
puts (x | y).inspect # [1, 2, 3, 4, 5, 6, 7] (union)
# Also useful for removing duplicates
names = ['Alice', 'Bob', 'Alice', 'Carol', 'Bob']
puts (names | []).inspect # ["Alice", "Bob", "Carol"] (no duplicates)
# Flattening nested arrays with flatten
nested = [1, [2, 3], [4, [5, 6]]]
puts nested.flatten.inspect # [1, 2, 3, 4, 5, 6] (fully flattened)
puts nested.flatten(1).inspect # [1, 2, 3, 4, [5, 6]] (one level deep)
# Pairing multiple arrays with zip
keys = [:name, :age, :city]
values = ['Taro', 30, 'Tokyo']
puts keys.zip(values).inspect
# [[:name, "Taro"], [:age, 30], [:city, "Tokyo"]]
# A common technique: convert zip result to a Hash
hash = keys.zip(values).to_h
puts hash.inspect # {:name=>"Taro", :age=>30, :city=>"Tokyo"}
Notes
There are two ways to concatenate arrays: + (non-destructive) and concat (destructive). + returns a new array and leaves the original unchanged, while concat modifies the original array in place. When working with large amounts of data, concat is more memory-efficient because it does not create a new object.
The set operators (-, &, |) automatically remove duplicates. They are useful for extracting common elements or checking differences. If your only goal is to remove duplicates, the uniq method is also available.
Think of zip as lining up multiple arrays side by side and combining them column by column. It is commonly used with to_h to build a Hash from separate key and value arrays. Combining it with each_with_index lets you write index-based processing concisely.
If you find any errors or copyright issues, please contact us.