Recall & Review
beginner
How do you sort an array in Ruby in ascending order?
Use the
sort method. For example, [3, 1, 2].sort returns [1, 2, 3].Click to reveal answer
beginner
How do you reverse the order of elements in an array in Ruby?
Use the
reverse method. For example, [1, 2, 3].reverse returns [3, 2, 1].Click to reveal answer
intermediate
What does the
sort! method do in Ruby?It sorts the array in place, changing the original array instead of creating a new one.
Click to reveal answer
intermediate
How can you sort an array in descending order in Ruby?
You can use
sort.reverse or sort { |a, b| b <=> a } to sort in descending order.Click to reveal answer
intermediate
What is the difference between
reverse and reverse! methods?reverse returns a new array with elements reversed, while reverse! reverses the elements in the original array itself.Click to reveal answer
Which Ruby method sorts an array without changing the original array?
✗ Incorrect
The
sort method returns a new sorted array and does not modify the original array.What does
[5, 3, 8].reverse return?✗ Incorrect
The
reverse method returns a new array with elements in reverse order.How do you sort an array in descending order in Ruby?
✗ Incorrect
Sorting ascending then reversing gives descending order.
Which method changes the original array when reversing it?
✗ Incorrect
reverse! modifies the original array in place.What does
sort! do?✗ Incorrect
sort! sorts the original array itself, changing it.Explain how to sort and reverse an array in Ruby, including how to do it without changing the original array.
Think about methods that return new arrays versus those that change the original.
You got /4 concepts.
Describe the difference between
sort and sort!, and when you might want to use each.Consider if you want to keep the original array unchanged or not.
You got /4 concepts.