Bird
0
0

You want to store user info with keys :name, :age, and :email. Which hash usage best supports easy updates and clear code?

hard📝 Application Q15 of 15
Ruby - Hashes
You want to store user info with keys :name, :age, and :email. Which hash usage best supports easy updates and clear code?
Auser = { name: 'Eve', age: 28, email: 'eve@example.com' }
Buser = [ 'name' => 'Eve', 'age' => 28, 'email' => 'eve@example.com' ]
Cuser = { 'name', 'Eve', 'age', 28, 'email', 'eve@example.com' }
Duser = ( name: 'Eve', age: 28, email: 'eve@example.com' )
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct hash syntax for storing key-value pairs

    user = { name: 'Eve', age: 28, email: 'eve@example.com' } uses a hash with symbol keys and values inside curly braces, which is clear and easy to update.
  2. Step 2: Evaluate other options for correctness and clarity

    user = [ 'name' => 'Eve', 'age' => 28, 'email' => 'eve@example.com' ] uses array syntax incorrectly, C is invalid syntax, and D uses parentheses which is not valid for hashes.
  3. Final Answer:

    user = { name: 'Eve', age: 28, email: 'eve@example.com' } -> Option A
  4. Quick Check:

    Curly braces with symbol keys = best practice [OK]
Quick Trick: Use curly braces and symbol keys for clear, update-friendly hashes [OK]
Common Mistakes:
  • Using arrays instead of hashes
  • Incorrect syntax with parentheses
  • Mixing key-value pairs without proper separators

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes