A symbol key is a lightweight, immutable identifier often used as a hash key. It is written with a colon, like :name, and is more memory-efficient than strings.
Click to reveal answer
beginner
How do string keys differ from symbol keys in Ruby hashes?
String keys are mutable objects and can be changed, while symbol keys are immutable and unique. String keys take more memory and processing time compared to symbol keys.
Click to reveal answer
intermediate
Why might you choose symbol keys over string keys in Ruby hashes?
Symbol keys are faster and use less memory because Ruby reuses the same symbol object. They are good when keys are fixed and repeated often, like configuration or options.
Click to reveal answer
intermediate
When is it better to use string keys instead of symbol keys?
Use string keys when keys come from user input or external sources, or when keys need to be mutable or manipulated as strings.
Click to reveal answer
beginner
What happens if you use string keys and symbol keys interchangeably in a Ruby hash?
They are treated as different keys. For example, { 'name' => 'Alice', :name => 'Bob' } has two separate entries because strings and symbols are not the same.
Click to reveal answer
Which of these is a symbol key in Ruby?
Aage:
B"age"
Cage
D:age
✗ Incorrect
A symbol key starts with a colon, like :age.
Why are symbol keys generally preferred over string keys for fixed hash keys?
AThey can store numbers
BThey are mutable and can be changed
CThey use less memory and are faster
DThey are easier to convert to strings
✗ Incorrect
Symbol keys are immutable and reused, so they use less memory and are faster.
What happens if you use both :name and "name" as keys in the same Ruby hash?
AThey are treated as different keys
BThey refer to the same key
CThey cause an error
DThe string key overwrites the symbol key
✗ Incorrect
Symbols and strings are different objects, so they create separate keys.
When should you prefer string keys over symbol keys?
AWhen keys are fixed and repeated
BWhen keys come from user input or external sources
CWhen you want faster access
DWhen keys are immutable
✗ Incorrect
String keys are better for dynamic or external data that might change.
Which statement about symbol keys is true?
ASymbols are unique and immutable
BSymbols are mutable and can be changed
CSymbols use more memory than strings
DSymbols cannot be used as hash keys
✗ Incorrect
Symbols are unique and immutable, making them efficient as hash keys.
Explain the main differences between symbol keys and string keys in Ruby hashes.
Think about memory, mutability, and when keys come from external sources.
You got /5 concepts.
Describe a situation where you would choose string keys over symbol keys in a Ruby program.
Consider dynamic data or external sources.
You got /3 concepts.
Practice
(1/5)
1. Which key type is best to use in a Ruby hash when the keys are fixed and known in advance?
easy
A. Symbols, because they are faster and use less memory.
B. Strings, because they are easier to type.
C. Integers, because they are numeric.
D. Arrays, because they can hold multiple values.
Solution
Step 1: Understand key types in Ruby hashes
Symbols are immutable and unique, making them efficient for fixed keys.
Step 2: Compare symbols and strings for fixed keys
Symbols use less memory and are faster than strings when keys do not change.
Final Answer:
Symbols, because they are faster and use less memory. -> Option A
Quick Check:
Fixed keys = Symbols [OK]
Hint: Use symbols for fixed keys to save memory and speed [OK]
Common Mistakes:
Using strings for fixed keys wastes memory
Confusing symbols with strings
Thinking integers or arrays are valid hash keys here
2. Which of the following is the correct NEW STYLE syntax to define a Ruby hash with symbol keys?
easy
A. { 'name' => 'Alice', 'age' => 30 }
B. { name: 'Alice', age: 30 }
C. { :name => 'Alice', :age => 30 }
D. { name => 'Alice', age => 30 }
Solution
Step 1: Recognize Ruby symbol key syntax
Ruby allows symbol keys with the syntax key: value (new style) or :key => value (old style).
Step 2: Identify the correct syntax among options
{ name: 'Alice', age: 30 } uses the modern symbol key syntax: name: 'Alice', age: 30.
Final Answer:
{ name: 'Alice', age: 30 } -> Option B
Quick Check:
Symbol keys use key: value syntax [OK]
Hint: Use key: value for symbol keys in Ruby hashes [OK]