Menu
Coddy logo textTech

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, 40

This 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, b

Negative 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, yellow

If 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, 3

Choose this method when you know the count of elements needed, and use ranges when you know the exact positions.

challenge icon

Challenge

Easy

Read three inputs:

  1. A single line of comma-separated integers (e.g., 5,10,15,20,25,30,35,40)
  2. A starting index (integer)
  3. 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
30

If the inputs are 100,200,300,400,500, 0, and 2, the output should be:

100
200

If the inputs are a,b,c,d,e,f,g, -3, and 2, the output should be:

e
f

If the inputs are 10,20,30, 1, and 10, the output should be:

20
30

Cheat 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, 40

The 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, yellow

If 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, 3

Try 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 line
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals