Array Operators
Part of the Fundamentals section of Coddy's Ruby journey — lesson 71 of 88.
Ruby provides special operators that let you combine, compare, and manipulate arrays in powerful ways. These operators make it easy to work with multiple arrays at once.
The + operator concatenates two arrays into a new one:
a = [1, 2, 3]
b = [4, 5, 6]
combined = a + b
puts combined # Outputs: 1, 2, 3, 4, 5, 6The - operator removes elements from the first array that appear in the second:
all_numbers = [1, 2, 3, 4, 5]
to_remove = [2, 4]
result = all_numbers - to_remove
puts result # Outputs: 1, 3, 5The & operator returns elements common to both arrays (intersection):
a = [1, 2, 3, 4]
b = [3, 4, 5, 6]
common = a & b
puts common # Outputs: 3, 4The | operator combines arrays and removes duplicates (union):
a = [1, 2, 3]
b = [3, 4, 5]
union = a | b
puts union # Outputs: 1, 2, 3, 4, 5All these operators create new arrays and leave the original arrays unchanged.
Challenge
EasyRead two lines of input, each containing comma-separated integers:
- The first array (e.g.,
1,2,3,4,5) - The second array (e.g.,
3,4,5,6,7)
Perform the following operations and print the results, each on its own line:
- Print each element of the concatenation of both arrays (using
+) - Print
---as a separator - Print each element of the difference (first array minus second, using
-) - Print
---as a separator - Print each element of the intersection (common elements, using
&) - Print
---as a separator - Print each element of the union (combined without duplicates, using
|)
For example, if the inputs are 1,2,3,4,5 and 3,4,5,6,7, the output should be:
1
2
3
4
5
3
4
5
6
7
---
1
2
---
3
4
5
---
1
2
3
4
5
6
7If the inputs are 10,20,30 and 30,40,50, the output should be:
10
20
30
30
40
50
---
10
20
---
30
---
10
20
30
40
50If the inputs are 5,10,15 and 1,2,3, the output should be:
5
10
15
1
2
3
---
5
10
15
---
---
5
10
15
1
2
3Cheat sheet
Ruby provides special operators for combining, comparing, and manipulating arrays:
The + operator concatenates two arrays:
a = [1, 2, 3]
b = [4, 5, 6]
combined = a + b # [1, 2, 3, 4, 5, 6]The - operator removes elements from the first array that appear in the second:
all_numbers = [1, 2, 3, 4, 5]
to_remove = [2, 4]
result = all_numbers - to_remove # [1, 3, 5]The & operator returns the intersection (common elements):
a = [1, 2, 3, 4]
b = [3, 4, 5, 6]
common = a & b # [3, 4]The | operator returns the union (combined without duplicates):
a = [1, 2, 3]
b = [3, 4, 5]
union = a | b # [1, 2, 3, 4, 5]All these operators create new arrays and leave the original arrays unchanged.
Try it yourself
# Read input arrays
input1 = gets.chomp.split(',').map(&:to_i)
input2 = gets.chomp.split(',').map(&:to_i)
# TODO: Write your code below
# Perform the following operations:
# 1. Concatenation using +
# 2. Difference using -
# 3. Intersection using &
# 4. Union using |
# Print each result with "---" as separator between operationsThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 42Variables and Data Types
Numbers and VariablesString Data TypeBoolean Data TypeSymbol Data TypeChecking Data TypesNaming ConventionsRecap - Variable Creation8Loops
For Loop with RangesWhile LoopBreakNextRecap - FactorialTimes LoopUntil LoopNested LoopsRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators6Basic IO
Output with putsOutput with print and pOutput With VariablesInput with getsChomp MethodType ConversionRecap - Age CalculatorRecap - True or False