Bird
0
0

You have a Ruby hash where keys come from user input and can vary. Which key type should you use and why?

hard📝 Application Q15 of 15
Ruby - Hashes
You have a Ruby hash where keys come from user input and can vary. Which key type should you use and why?
user_data = { ??? }
AUse symbols because they are faster and memory efficient.
BUse arrays as keys to hold multiple user inputs.
CUse strings because user input keys can change dynamically.
DUse integers because they are simple and fixed.
Step-by-Step Solution
Solution:
  1. Step 1: Understand key type suitability for dynamic keys

    Symbols are immutable and reused, so creating many from user input can cause memory bloat.
  2. Step 2: Choose key type for user input keys

    Strings are better for dynamic or user input keys because they do not create permanent symbols.
  3. Final Answer:

    Use strings because user input keys can change dynamically. -> Option C
  4. Quick Check:

    Dynamic keys = Strings [OK]
Quick Trick: Use strings for keys from user input to avoid memory issues [OK]
Common Mistakes:
  • Using symbols for dynamic keys causing memory leaks
  • Assuming integers or arrays are suitable keys here
  • Ignoring key type impact on memory and performance

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes