Array#intersection
array & other_array -> array Returns:
Array · Updated March 13, 2026 · Array Methods arrays set-operations intersection
The intersection method returns a new array containing elements that appear in all the arrays. When you use the & operator, it performs the same operation between exactly two arrays.
Syntax
array & other_array
array.intersection(*other_arrays)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
other_array | Array | — | The array to intersect with |
*other_arrays | Array | — | Additional arrays (for the method form) |
Examples
Basic usage with the & operator
a = [1, 2, 3, 4, 5]
b = [3, 4, 5, 6, 7]
a & b
# => [3, 4, 5]
Using the method form with multiple arrays
a = [1, 2, 3, 4]
b = [2, 3, 5, 6]
c = [2, 3, 7, 8]
a.intersection(b, c)
# => [2, 3]
Handling duplicate values
a = [1, 1, 2, 2, 3]
b = [1, 2, 2, 3, 3]
a & b
# => [1, 2, 3]
The result contains each common element only once, even if it appeared multiple times in the original arrays.
Common Patterns
Filtering array contents
# Keep only elements that exist in a whitelist
allowed = [1, 2, 3]
user_input = [1, 4, 2, 5, 3, 6]
user_input & allowed
# => [1, 2, 3]
Set operations
# Find common tags between two posts
post1_tags = ["ruby", "rails", "tutorial"]
post2_tags = ["ruby", "rails", "performance"]
common = post1_tags & post2_tags
# => ["ruby", "rails"]