A hash is a collection of key-value pairs, like a dictionary, where you can look up a value using its key.
Click to reveal answer
beginner
How do you create a hash with symbol keys in Ruby?
You can create it using curly braces and symbol keys with colons, like: { name: "Alice", age: 30 }.
Click to reveal answer
beginner
How do you create a hash with string keys in Ruby?
Use curly braces with strings as keys and hash rockets, like: { "name" => "Alice", "age" => 30 }.
Click to reveal answer
intermediate
What is the difference between symbol keys and string keys in Ruby hashes?
Symbol keys are immutable and often faster, while string keys are mutable. Symbols are written without quotes and strings with quotes.
Click to reveal answer
intermediate
Show an example of a Ruby hash mixing symbol and string keys.
Example: { name: "Alice", "age" => 30 }. You can mix keys but be careful when accessing values.
Click to reveal answer
Which of these is a valid Ruby hash with symbol keys?
A{ name: "Bob", city: "NY" }
B{ "name" => "Bob", "city" => "NY" }
C[ name: "Bob", city: "NY" ]
D( name: "Bob", city: "NY" )
✗ Incorrect
Option A uses symbol keys with colons inside curly braces, which is the correct syntax for symbol-key hashes.
How do you access the value for key :name in hash { name: "Eve" }?
Ahash["name"]
Bhash[:name]
Chash.name
Dhash->name
✗ Incorrect
You access symbol keys using the symbol itself inside square brackets, like hash[:name].
Which symbol key is equivalent to the string key "age" in a Ruby hash?
A:age
B"age"
Cage
Dage:
✗ Incorrect
The symbol :age is a different type from the string "age" but often used as a key in hashes.
What happens if you mix symbol and string keys in a Ruby hash?
AThey are treated as the same key
BThe string key overrides the symbol key
CThey are treated as different keys
DRuby throws an error
✗ Incorrect
Symbol and string keys are different objects, so they are treated as different keys in a hash.
Which syntax creates a hash with string keys?
A{ color: "red", size: "M" }
B( color: "red", size: "M" )
C[ "color" => "red", "size" => "M" ]
D{ "color" => "red", "size" => "M" }
✗ Incorrect
Using strings as keys requires the hash rocket => syntax inside curly braces.
Explain how to create a Ruby hash using symbol keys and how to access its values.
Think about keys without quotes and how you get values.
You got /3 concepts.
Describe the difference between using string keys and symbol keys in Ruby hashes.
Consider how keys look and behave.
You got /4 concepts.
Practice
(1/5)
1. Which of the following is a valid way to create 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: Understand symbol key syntax
In Ruby, symbol keys can be written as name: value without the rocket (=>) syntax.
Step 2: Check each option's syntax
{ name: "Alice", age: 30 } uses symbol keys with colon syntax correctly. { "name": "Alice", "age": 30 } uses string keys with colon, which is invalid. { :name => "Alice", "age": 30 } uses rocket syntax for the symbol key correctly but attempts colon syntax after a string key, which is invalid. { name => "Alice", age => 30 } uses bare words without colon or rocket, which is invalid.
Final Answer:
{ name: "Alice", age: 30 } -> Option D
Quick Check:
Symbol keys use colon syntax = { name: "Alice", age: 30 } [OK]
Hint: Symbol keys use colon after name, not quotes or bare words [OK]
Common Mistakes:
Using quotes around symbol keys with colon syntax
Omitting colon or rocket for keys
Mixing string and symbol keys without proper syntax
2. Which of the following hash definitions will cause a syntax error in Ruby?
easy
A. { name => "Bob", age => 25 }
B. { :name => "Bob", :age => 25 }
C. { "name" => "Bob", "age" => 25 }
D. { name: "Bob", age: 25 }
Solution
Step 1: Review valid key syntax
Symbol keys can be written as :name => or name:. String keys must be quoted and use rocket syntax.
Step 2: Identify invalid syntax
{ name => "Bob", age => 25 } uses bare words as keys without colon or rocket, which is invalid and causes syntax error.
Final Answer:
SyntaxError -> Option A
Quick Check:
Bare words as keys need colon or rocket [OK]
Hint: Keys must be symbols with colon or strings with quotes and rocket [OK]
The hash has a symbol key :name and a string key "age". Accessing with matching key types returns values.
Step 2: Evaluate each puts statement
person[:name] returns "Carol". person["age"] returns 28. Both print on separate lines.
Final Answer:
Carol
28 -> Option A
Quick Check:
Match key types to get values = Carol
28 [OK]
Hint: Use matching symbol or string keys to access hash values [OK]
Common Mistakes:
Accessing string key with symbol or vice versa
Expecting mixed keys to be interchangeable
Forgetting keys are type-sensitive
4. Identify the error in this Ruby hash creation:
data = { :city => "Paris", country: "France", "population" => 2148327, state }
medium
A. Hash keys cannot mix symbols and strings
B. Incorrect symbol syntax for :city
C. Missing value for key 'state'
D. String keys must use colon syntax
Solution
Step 1: Check each key-value pair
Keys :city, country, and "population" have values. The key 'state' is written without a value, which is invalid.
Step 2: Identify syntax error
Every key in a hash must have a corresponding value. Missing value for 'state' causes syntax error.
Final Answer:
Missing value for key 'state' -> Option C
Quick Check:
All keys need values in hash = Missing value for key 'state' [OK]
Hint: Every key must have a value; check for missing pairs [OK]
Common Mistakes:
Leaving keys without values
Confusing symbol and string key syntax
Assuming mixed keys cause errors
5. Given the array arr = [["name", "Dave"], [:age, 40], ["city", "NY"]], which code correctly converts it into a hash with symbol keys where possible?
hard
A. hash = arr.to_h
B. hash = arr.map { |k, v| [k.to_sym, v] }.to_h
C. hash = arr.map { |k, v| [k, v] }.to_h
D. hash = arr.map { |k, v| [k.to_s, v] }.to_h
Solution
Step 1: Understand the array structure
The array has pairs with keys as strings or symbols. We want all keys as symbols where possible.
Step 2: Convert keys to symbols
hash = arr.map { |k, v| [k.to_sym, v] }.to_h converts each key to symbol using k.to_sym before creating the hash, ensuring keys are symbols.
Step 3: Check other options
hash = arr.to_h keeps keys as is (mixed). hash = arr.map { |k, v| [k, v] }.to_h keeps keys as is without conversion. hash = arr.map { |k, v| [k.to_s, v] }.to_h converts all keys to strings, not symbols.