0
0
Rubyprogramming~10 mins

Array slicing and ranges in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Array slicing and ranges
Start with array
Define range or slice indices
Extract elements using slice or range
Return new array with selected elements
Use or print sliced array
This flow shows how Ruby extracts parts of an array using slice indices or ranges, returning a new array with those elements.
Execution Sample
Ruby
arr = [10, 20, 30, 40, 50]
slice1 = arr[1..3]
slice2 = arr[2, 2]
puts slice1.inspect
puts slice2.inspect
This code slices the array using a range and using start index with length, then prints the results.
Execution Table
StepActionExpressionResultOutput
1Define arrayarr = [10, 20, 30, 40, 50][10, 20, 30, 40, 50]
2Slice with rangeslice1 = arr[1..3][20, 30, 40]
3Slice with start and lengthslice2 = arr[2, 2][30, 40]
4Print slice1puts slice1.inspect[20, 30, 40]
5Print slice2puts slice2.inspect[30, 40]
6EndNo more code
💡 All slices extracted and printed; program ends.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
arr[10, 20, 30, 40, 50][10, 20, 30, 40, 50][10, 20, 30, 40, 50][10, 20, 30, 40, 50]
slice1nil[20, 30, 40][20, 30, 40][20, 30, 40]
slice2nilnil[30, 40][30, 40]
Key Moments - 2 Insights
Why does arr[1..3] include the element at index 3?
arr[1..3] uses a range which includes both start and end indices (1 to 3), so it includes index 3. arr[2, 2] uses start index 2 and length 2, so it includes indices 2 and 3 only, not index 4.
What happens if the range goes beyond the array length, like arr[3..10]?
Ruby returns elements from index 3 up to the end of the array without error, so it safely slices up to the array's last element.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of slice1 after step 2?
A[20, 30, 40]
B[30, 40]
C[10, 20, 30]
Dnil
💡 Hint
Check the 'Result' column at step 2 in the execution_table.
At which step is slice2 assigned a value?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the assignment of slice2 in the 'Action' column.
If we change arr[2, 2] to arr[2..3], what would slice2 contain?
A[20, 30]
B[30, 40]
C[30, 40, 50]
D[40, 50]
💡 Hint
Compare how ranges and start,length slicing work in the variable_tracker.
Concept Snapshot
Array slicing in Ruby:
- arr[start..end] extracts elements from start to end index inclusive.
- arr[start, length] extracts length elements starting at start index.
- If range exceeds array length, slicing stops at array end.
- Returns a new array with selected elements.
- Useful for extracting parts of arrays easily.
Full Transcript
This visual trace shows how Ruby slices arrays using ranges and start-length pairs. We start with an array of five numbers. Using arr[1..3], Ruby extracts elements at indices 1, 2, and 3, returning [20, 30, 40]. Using arr[2, 2], Ruby extracts two elements starting at index 2, returning [30, 40]. The execution table tracks each step, showing variable values and outputs. Key moments clarify why ranges include the end index and how slicing beyond array length works safely. The quiz tests understanding of slice values and differences between slicing methods. The snapshot summarizes the syntax and behavior for quick reference.