Recall & Review
beginner
What does array slicing mean in Ruby?
Array slicing means taking a part of an array by specifying a start position and length or a range. It creates a new array with the selected elements.
Click to reveal answer
beginner
How do you use a range to slice an array in Ruby?
You use a range inside square brackets, like array[start..end], to get elements from start to end positions inclusive.
Click to reveal answer
intermediate
What is the difference between .. and ... in Ruby ranges when slicing arrays?
.. includes the end index, ... excludes the end index in the range.
Click to reveal answer
beginner
Given array = [10, 20, 30, 40, 50], what is array[1, 3]?
It returns [20, 30, 40]. It starts at index 1 and takes 3 elements.
Click to reveal answer
intermediate
How does negative indexing work in array slicing?
Negative indexes count from the end. For example, array[-2..-1] gets the last two elements.
Click to reveal answer
What does array[2..4] return if array = [5, 10, 15, 20, 25, 30]?
✗ Incorrect
The range 2..4 includes indexes 2, 3, and 4, so elements 15, 20, and 25 are returned.
What is the result of array[1, 2] for array = [7, 14, 21, 28]?
✗ Incorrect
Starting at index 1, take 2 elements: 14 and 21.
Which range excludes the end index when slicing?
✗ Incorrect
The ... range excludes the end index, unlike .. which includes it.
What does array[-3..-1] return for array = [1, 2, 3, 4, 5]?
✗ Incorrect
Negative indexes count from the end: -3 is 3rd last element, so elements 3, 4, 5 are returned.
What happens if you use array[5, 2] on array = [10, 20, 30]?
✗ Incorrect
Index 5 is beyond array length, so slicing returns an empty array.
Explain how to slice an array using a range in Ruby and the difference between .. and ...
Think about how you select parts of an array by index.
You got /4 concepts.
Describe how negative indexes work in array slicing and give an example.
Remember last element is -1.
You got /3 concepts.