Menu
Coddy logo textTech

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

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

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

This 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 icon

Challenge

Easy

Read two inputs:

  1. A single line of comma-separated integers (e.g., 10,20,30,40,50,60,70)
  2. A range specification in the format start..end or start...end (e.g., 2..5 or 1...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
60

If the inputs are 5,10,15,20,25,30 and 0...3, the output should be:

5
10
15

If the inputs are 100,200,300,400,500 and 1..-1, the output should be:

200
300
400
500

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

d
e
f

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

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

All lessons in Fundamentals