Ruby - HashesWhy does Ruby allow both symbol and string keys in hashes, and what is a key difference in their behavior?ASymbols are immutable and reused, strings are mutable and unique objectsBSymbols and strings behave identically in hashesCSymbols are slower to access than stringsDStrings are automatically converted to symbols in hashesCheck Answer
Step-by-Step SolutionSolution:Step 1: Understand symbol immutabilitySymbols are immutable and reused, meaning the same symbol object is used everywhere.Step 2: Understand string mutabilityStrings are mutable and each string literal creates a new object, so keys differ by object identity.Final Answer:Symbols are immutable and reused, strings are mutable and unique objects -> Option AQuick Check:Symbols immutable, strings mutable [OK]Quick Trick: Symbols are immutable; strings are mutable objects [OK]Common Mistakes:Assuming symbols and strings behave the sameThinking strings convert automatically to symbolsBelieving symbols are slower than strings
Master "Hashes" in Ruby9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Ruby Quizzes Arrays - Array slicing and ranges - Quiz 3easy Control Flow - Case/when statement - Quiz 13medium Hashes - Accessing and setting values - Quiz 1easy Methods - Bang methods (ending with !) - Quiz 12easy Methods - Why methods always return a value in Ruby - Quiz 12easy Operators and Expressions - Logical operators (&&, ||, !) - Quiz 15hard Ruby Basics and Runtime - Everything is an object mental model - Quiz 14medium String Operations - Heredoc syntax for multiline strings - Quiz 5medium Variables and Data Types - Integer and Float number types - Quiz 6medium Variables and Data Types - Local variables and naming conventions - Quiz 12easy