Bird
Raised Fist0
Rubyprogramming~10 mins

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

Choose your learning style10 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.

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

  1. Step 1: Understand the range operator '..'

    The range 1..3 includes both 1 and 3, so it covers indexes 1, 2, and 3.
  2. 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.
  3. Final Answer:

    Returns a new array with elements at indexes 1, 2, and 3 -> Option A
  4. 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

  1. Step 1: Identify the range operator for excluding end

    In Ruby, ... excludes the end index, so 2...5 means indexes 2, 3, and 4.
  2. Step 2: Check syntax correctness

    Only arr[2...5] uses valid Ruby syntax for slicing excluding the end index.
  3. Final Answer:

    arr[2...5] -> Option A
  4. Quick Check:

    Use '...' to exclude end index in range [OK]
Hint: Use '...' to exclude the last index in range slicing [OK]
Common Mistakes:
  • Using '..' when exclusion is needed
  • Using Python-style slicing syntax like ':'
  • Using invalid range syntax like '2-5'
3. What is the output of the following Ruby code?
arr = [5, 10, 15, 20, 25]
slice = arr[1...4]
puts slice.inspect
medium
A. [10, 15, 20, 25]
B. [15, 20, 25]
C. [10, 15, 20]
D. [5, 10, 15]

Solution

  1. Step 1: Understand the slicing range

    The range 1...4 includes indexes 1, 2, and 3 but excludes 4.
  2. Step 2: Extract elements at these indexes

    Elements at indexes 1, 2, 3 are 10, 15, and 20 respectively.
  3. Final Answer:

    [10, 15, 20] -> Option C
  4. Quick Check:

    Range '...' excludes end index = [10, 15, 20] [OK]
Hint: Remember '...' excludes the last index in slicing [OK]
Common Mistakes:
  • Including element at index 4 mistakenly
  • Confusing indexes with values
  • Printing original array instead of slice
4. Identify the error in the following Ruby code snippet:
arr = [1, 2, 3, 4, 5]
slice = arr[2..6]
puts slice.inspect
medium
A. Syntax error due to invalid range
B. Range end index 6 is out of bounds but Ruby returns elements up to the end of the array
C. Runtime error because index 6 does not exist
D. slice will be empty array because range is invalid

Solution

  1. Step 1: Check the range indexes against array size

    The array has indexes 0 to 4. The range 2..6 goes beyond the last index.
  2. Step 2: Understand Ruby slicing behavior for out-of-bounds

    Ruby returns elements from index 2 to the end (indexes 2, 3, 4) and ignores indexes beyond 4 without error.
  3. Final Answer:

    Range end index 6 is out of bounds but Ruby returns elements up to the end of the array -> Option B
  4. Quick Check:

    Ruby slices safely even if range exceeds array size [OK]
Hint: Ruby slicing ignores out-of-bound indexes without error [OK]
Common Mistakes:
  • Expecting runtime or syntax error
  • Assuming slice will be empty
  • Confusing Ruby with other languages that error here
5. Given arr = [2, 4, 6, 8, 10, 12], which expression returns a new array with every second element starting from index 1 up to index 5?
hard
A. arr[1..5].select.with_index { |_, i| i.even? }
B. arr[1..5].select.with_index { |_, i| i.odd? }
C. arr[1...6].values_at(*arr[1...6].each_index.select(&:even?))
D. arr[1..5].values_at(*arr[1..5].each_index.select(&:odd?))

Solution

  1. Step 1: Slice array from index 1 to 5

    arr[1..5] gives [4, 6, 8, 10, 12].
  2. Step 2: Select every second element starting from index 1 of the slice

    Indexes inside the slice are 0 to 4. Selecting odd indexes (1, 3) gives elements 6 and 10.
  3. Step 3: Check options for correct syntax and output

    arr[1..5].values_at(*arr[1..5].each_index.select(&:odd?)) uses values_at with odd indexes on the slice correctly, returning [6, 10].
  4. Final Answer:

    arr[1..5].values_at(*arr[1..5].each_index.select(&:odd?)) -> Option D
  5. Quick Check:

    Use values_at with odd indexes on slice for every second element [OK]
Hint: Use values_at with selected indexes for custom slicing [OK]
Common Mistakes:
  • Confusing odd/even indexes inside the slice
  • Using select without values_at returns elements, not indexes
  • Using wrong range operator causing off-by-one errors
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard