0
0
Rubyprogramming~15 mins

Array slicing and ranges in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Array slicing and ranges
📖 Scenario: You are working with a list of daily temperatures recorded over a week. You want to extract specific parts of this list to analyze temperature trends.
🎯 Goal: Learn how to use array slicing and ranges in Ruby to select parts of an array.
📋 What You'll Learn
Create an array called temperatures with exact values for 7 days
Create a variable called start_day to mark the beginning index for slicing
Use array slicing with a range to get a subarray of temperatures
Print the sliced array to show the selected temperatures
💡 Why This Matters
🌍 Real World
Selecting parts of data from a list is common when analyzing time series like temperatures, sales, or website visits.
💼 Career
Understanding array slicing and ranges helps in data processing, filtering, and preparing data for reports or visualizations.
Progress0 / 4 steps
1
Create the temperatures array
Create an array called temperatures with these exact values: 22, 24, 19, 23, 25, 20, 18
Ruby
Need a hint?

Use square brackets [] to create an array and separate values with commas.

2
Set the start day index
Create a variable called start_day and set it to 2 to mark the starting index for slicing
Ruby
Need a hint?

Just assign the number 2 to the variable start_day.

3
Slice the array using a range
Create a variable called selected_temps and assign it the slice of temperatures from start_day to index 4 using a range
Ruby
Need a hint?

Use array[start..end] syntax to slice with a range.

4
Print the selected temperatures
Write a puts statement to print the selected_temps array
Ruby
Need a hint?

Use puts selected_temps.inspect to print the array in a readable format.