📖 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
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
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
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
Hint
Use puts selected_temps.inspect to print the array in a readable format.
Practice
(1/5)
1. What does the expression arr[1..3] do when arr = [10, 20, 30, 40, 50]?
easy
A. Returns a new array with elements at indexes 1, 2, and 3
B. Returns a new array with elements at indexes 1 and 3 only
C. Returns a new array with elements at indexes 1 and 2 only
D. Returns a single element at index 3
Solution
Step 1: Understand the range operator '..'
The range 1..3 includes both 1 and 3, so it covers indexes 1, 2, and 3.
Step 2: Apply slicing to the array
Slicing arr[1..3] returns elements at indexes 1, 2, and 3, which are 20, 30, and 40.
Final Answer:
Returns a new array with elements at indexes 1, 2, and 3 -> Option A
Quick Check:
Range '..' includes end index = elements 20, 30, 40 [OK]
Hint: Remember '..' includes the end index in slicing [OK]
Common Mistakes:
Thinking '..' excludes the end index
Confusing indexes with values
Assuming slicing changes the original array
2. Which of the following is the correct syntax to get elements from index 2 up to but not including index 5 in Ruby array arr?
easy
A. arr[2...5]
B. arr[2:5]
C. arr[2..5]
D. arr[2-5]
Solution
Step 1: Identify the range operator for excluding end
In Ruby, ... excludes the end index, so 2...5 means indexes 2, 3, and 4.
Step 2: Check syntax correctness
Only arr[2...5] uses valid Ruby syntax for slicing excluding the end index.
Final Answer:
arr[2...5] -> Option A
Quick Check:
Use '...' to exclude end index in range [OK]
Hint: Use '...' to exclude the last index in range slicing [OK]