Bird
0
0

What does the Ruby method sort_by do?

easy📝 Conceptual Q11 of 15
Ruby - Enumerable and Collection Processing
What does the Ruby method sort_by do?
ASorts elements based on a value you specify for each element.
BChanges the original array to a sorted one.
CRemoves duplicate elements from an array.
DReverses the order of elements in an array.
Step-by-Step Solution
Solution:
  1. Step 1: Understand the purpose of sort_by

    sort_by sorts elements by a value you choose for each element, like length or age.
  2. Step 2: Compare with other array methods

    Unlike sort!, sort_by returns a new sorted array without changing the original.
  3. Final Answer:

    Sorts elements based on a value you specify for each element. -> Option A
  4. Quick Check:

    sort_by sorts by chosen value [OK]
Quick Trick: Remember: sort_by sorts by a custom value, not in-place [OK]
Common Mistakes:
  • Thinking sort_by changes the original array
  • Confusing sort_by with reverse or uniq
  • Assuming sort_by removes duplicates

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes