0
0
Rubyprogramming~5 mins

Array slicing and ranges in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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]?
A[15, 20, 25]
B[15, 20]
C[20, 25, 30]
D[10, 15, 20]
What is the result of array[1, 2] for array = [7, 14, 21, 28]?
A[14, 21, 28]
B[14, 21]
C[21, 28]
D[7, 14]
Which range excludes the end index when slicing?
A-..
B..
C...
D-...
What does array[-3..-1] return for array = [1, 2, 3, 4, 5]?
A[3, 4, 5]
B[2, 3, 4]
C[1, 2, 3]
D[4, 5]
What happens if you use array[5, 2] on array = [10, 20, 30]?
ARaises an error
BReturns nil
CReturns [nil, nil]
DReturns [] (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.