Array Slicing with Ranges
Part of the Fundamentals section of Coddy's Ruby journey — lesson 69 of 88.
Sometimes you need to extract a portion of an array rather than accessing individual elements. Ruby lets you slice arrays using ranges, which creates a new array containing only the elements you specify.
To slice an array with a range, place the range inside square brackets:
numbers = [10, 20, 30, 40, 50]
slice = numbers[1..3]
puts slice # Outputs: 20, 30, 40The range 1..3 includes elements at indexes 1, 2, and 3. Remember that array indexes start at 0, so index 1 is the second element.
You can also use the three-dot range (...) to exclude the last index:
letters = ["a", "b", "c", "d", "e"]
slice = letters[0...3]
puts slice # Outputs: a, b, cHere, 0...3 includes indexes 0, 1, and 2, but stops before index 3.
Negative indexes work with ranges too. They count from the end of the array, where -1 is the last element:
colors = ["red", "green", "blue", "yellow"]
slice = colors[1..-1]
puts slice # Outputs: green, blue, yellowThis extracts everything from index 1 to the last element, which is useful when you want to skip the first element without knowing the array's length.
Challenge
EasyRead two inputs:
- A single line of comma-separated integers (e.g.,
10,20,30,40,50,60,70) - A range specification in the format
start..endorstart...end(e.g.,2..5or1...4)
Split the first input into an array of integers. Then use the range from the second input to slice the array and print each element of the sliced portion on its own line.
The range input will use either two dots (..) to include the end index, or three dots (...) to exclude it.
For example, if the inputs are 10,20,30,40,50,60,70 and 2..5, the output should be:
30
40
50
60If the inputs are 5,10,15,20,25,30 and 0...3, the output should be:
5
10
15If the inputs are 100,200,300,400,500 and 1..-1, the output should be:
200
300
400
500If the inputs are a,b,c,d,e,f and -3..-1, the output should be:
d
e
fCheat sheet
To extract a portion of an array, use ranges inside square brackets:
numbers = [10, 20, 30, 40, 50]
slice = numbers[1..3]
# Returns [20, 30, 40] (indexes 1, 2, and 3)Use two dots (..) to include the end index:
letters = ["a", "b", "c", "d", "e"]
slice = letters[0..2]
# Returns ["a", "b", "c"] (indexes 0, 1, and 2)Use three dots (...) to exclude the end index:
letters = ["a", "b", "c", "d", "e"]
slice = letters[0...3]
# Returns ["a", "b", "c"] (indexes 0, 1, and 2, stops before 3)Negative indexes count from the end of the array (-1 is the last element):
colors = ["red", "green", "blue", "yellow"]
slice = colors[1..-1]
# Returns ["green", "blue", "yellow"] (from index 1 to the last element)Try it yourself
# Read the comma-separated values
input_values = gets.chomp
# Read the range specification
range_spec = gets.chomp
# TODO: Write your code below
# 1. Split the input_values into an array
# 2. Parse the range_spec to determine start, end, and whether to use .. or ...
# 3. Slice the array using the appropriate range
# 4. Print each element of the sliced portion on its own lineThis 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