Array Slicing with Indexes
Part of the Fundamentals section of Coddy's Ruby journey — lesson 70 of 88.
Ruby offers another way to slice arrays using a starting index and a length. Instead of specifying a range, you provide two numbers: where to start and how many elements to extract.
The syntax uses a comma to separate the start index from the count:
numbers = [10, 20, 30, 40, 50]
slice = numbers[1, 3]
puts slice # Outputs: 20, 30, 40This extracts 3 elements starting from index 1. The first number is the starting position, and the second is how many elements you want.
This approach is especially useful when you know exactly how many elements you need:
letters = ["a", "b", "c", "d", "e"]
first_two = letters[0, 2]
puts first_two # Outputs: a, bNegative indexes work here too. You can start from the end of the array:
colors = ["red", "green", "blue", "yellow"]
last_two = colors[-2, 2]
puts last_two # Outputs: blue, yellowIf you request more elements than available, Ruby simply returns what exists without raising an error:
nums = [1, 2, 3]
slice = nums[1, 10]
puts slice # Outputs: 2, 3Choose this method when you know the count of elements needed, and use ranges when you know the exact positions.
Challenge
EasyRead three inputs:
- A single line of comma-separated integers (e.g.,
5,10,15,20,25,30,35,40) - A starting index (integer)
- The number of elements to extract (integer)
Split the first input into an array of integers. Use the starting index and count to slice the array using the index-and-length syntax, then print each element of the sliced portion on its own line.
For example, if the inputs are 5,10,15,20,25,30,35,40, 2, and 4, the output should be:
15
20
25
30If the inputs are 100,200,300,400,500, 0, and 2, the output should be:
100
200If the inputs are a,b,c,d,e,f,g, -3, and 2, the output should be:
e
fIf the inputs are 10,20,30, 1, and 10, the output should be:
20
30Cheat sheet
Ruby allows slicing arrays using a starting index and a length. The syntax uses a comma to separate the start index from the count:
array[start_index, length]Basic example:
numbers = [10, 20, 30, 40, 50]
slice = numbers[1, 3]
puts slice # Outputs: 20, 30, 40The first number is the starting position, and the second is how many elements to extract.
Negative indexes work with this syntax, allowing you to start from the end of the array:
colors = ["red", "green", "blue", "yellow"]
last_two = colors[-2, 2]
puts last_two # Outputs: blue, yellowIf you request more elements than available, Ruby returns what exists without raising an error:
nums = [1, 2, 3]
slice = nums[1, 10]
puts slice # Outputs: 2, 3Try it yourself
# Read inputs
input_line = gets.chomp
start_index = gets.chomp.to_i
count = gets.chomp.to_i
# Split the input into an array
arr = input_line.split(',')
# TODO: Write your code below to slice the array using index-and-length syntax
# and print each element 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