Bird
0
0

What is the primary purpose of Ruby's sort_by method?

easy📝 Conceptual Q1 of 15
Ruby - Enumerable and Collection Processing
What is the primary purpose of Ruby's sort_by method?
ATo sort elements based on a computed key for each element
BTo reverse the order of elements in an array
CTo remove duplicate elements from an array
DTo shuffle elements randomly in an array
Step-by-Step Solution
Solution:
  1. Step 1: Understand sort_by

    The method sorts elements by evaluating a block that returns a key for each element.
  2. Step 2: Compare keys

    Elements are sorted based on these keys, not the elements themselves.
  3. Final Answer:

    To sort elements based on a computed key for each element -> Option A
  4. Quick Check:

    sort_by uses keys for sorting [OK]
Quick Trick: sort_by sorts using keys returned from a block [OK]
Common Mistakes:
  • Confusing sort_by with shuffle or reverse
  • Thinking sort_by removes duplicates

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes