Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q6 of 15
Ruby - Enumerable and Collection Processing
Identify the error in this code snippet:
users = [{name: "Sam", age: 25}, {name: "Ann", age: 30}]
sorted = users.sort_by { |user| user.age }
AIncorrect access of age attribute on hash
BMissing parentheses in sort_by call
CBlock variable should be plural
DNo error, code is correct
Step-by-Step Solution
Solution:
  1. Step 1: Check data structure and attribute access

    Users is an array of hashes, so access age with user[:age], not user.age.
  2. Step 2: Identify syntax error

    Using dot notation on a hash causes a NoMethodError.
  3. Final Answer:

    Incorrect access of age attribute on hash -> Option A
  4. Quick Check:

    Hash keys accessed with brackets = Incorrect access of age attribute on hash [OK]
Quick Trick: Use brackets for hash keys, dot for object attributes [OK]
Common Mistakes:
  • Using dot notation on hashes
  • Assuming arrays have attributes
  • Ignoring error messages

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes