What happens when you access a missing key in a Ruby hash without a default value?
Ruby returns nil when you access a key that does not exist in the hash and no default value is set.
Click to reveal answer
beginner
How do you set a default value for missing keys in a Ruby hash?
You can set a default value by passing it when creating the hash, like Hash.new(default_value). This value is returned for any missing key.
Click to reveal answer
intermediate
What is the difference between Hash.new(default_value) and hash[key] ||= default_value?
Hash.new(default_value) sets a default for all missing keys automatically. hash[key] ||= default_value sets the value only when you access a missing key and want to assign a default at that moment.
Click to reveal answer
intermediate
How can you use a block to set default values dynamically in a Ruby hash?
You can create a hash with a block like Hash.new { |hash, key| hash[key] = some_value }. This block runs when a missing key is accessed, allowing dynamic default values.
Click to reveal answer
advanced
Why might using a mutable object as a default value cause unexpected behavior?
If you use a mutable object (like an array) as a default value, the same object is shared for all missing keys, so changes affect all keys. Using a block to create a new object each time avoids this.
Click to reveal answer
What does Hash.new(0) do in Ruby?
ARaises an error for missing keys
BCreates a hash with one key 0
CReturns nil for missing keys
DReturns 0 for any missing key
✗ Incorrect
Using Hash.new(0) sets the default value to 0, so any missing key returns 0.
How do you create a hash that assigns a new empty array for each missing key?
AHash.new([])
BHash.new { |h, k| h[k] = [] }
CHash.new(nil)
DHash.new(0)
✗ Incorrect
Using a block with Hash.new { |h, k| h[k] = [] } creates a new array for each missing key, avoiding shared mutable objects.
What will this code output? h = Hash.new('default'); puts h[:missing].object_id == h[:another_missing].object_id
Anil
Bfalse
Ctrue
DError
✗ Incorrect
The default object is shared, so both missing keys return the same object, making their object_ids equal.
Which method sets a default value only when a missing key is accessed and assigned?
Ahash[key] ||= default
BHash.new(default)
Chash.fetch(key, default)
Dhash.default = default
✗ Incorrect
hash[key] ||= default assigns the default value only if the key is missing or falsey.
What is the default return value of a Ruby hash for a missing key if no default is set?
Anil
Bempty string
Cfalse
D0
✗ Incorrect
By default, Ruby hashes return nil for keys that do not exist.
Explain how to set and use default values for missing keys in a Ruby hash.
Think about how Ruby handles missing keys and how you can customize that.
You got /4 concepts.
Describe why using a mutable object as a default value can cause problems and how to avoid it.
Consider what happens if you change the default object.
You got /4 concepts.
Practice
(1/5)
1. What does setting a default value for a Ruby hash do when you access a key that does not exist? h = Hash.new(0)
easy
A. Returns the default value instead of nil for missing keys
B. Raises an error for missing keys
C. Adds the missing key with nil value to the hash
D. Returns nil for missing keys as usual
Solution
Step 1: Understand default value behavior
When a hash is created with Hash.new(0), it sets 0 as the default value for any missing key.
Step 2: Accessing a missing key
Accessing a key not present in the hash returns the default value 0 instead of nil.
Final Answer:
Returns the default value instead of nil for missing keys -> Option A
Quick Check:
Default value = 0 returned for missing keys [OK]
Hint: Default value replaces nil for missing keys [OK]
Common Mistakes:
Thinking missing keys raise errors
Assuming missing keys return nil always
Believing missing keys get added automatically
2. Which of the following is the correct syntax to create a Ruby hash with a default value of an empty array for missing keys?
easy
A. h = Hash.new { |key| [] }
B. h = Hash.new { [] }
C. h = Hash.new([])
D. h = Hash.new { |hash, key| hash[key] = [] }
Solution
Step 1: Understand default block usage
Using a block with Hash.new allows dynamic default values. To assign a new array to each missing key, the block must assign it to the hash.
Step 2: Identify correct block syntax
The block receives hash and key parameters and assigns [] to hash[key]. This ensures each missing key gets its own array.
Final Answer:
h = Hash.new { |hash, key| hash[key] = [] } -> Option D
Quick Check:
Block assigns new array to missing keys [OK]
Hint: Use block with two params to assign default arrays [OK]
Common Mistakes:
Using Hash.new([]) shares one array for all keys
Using block without assigning to hash[key]
Using wrong block parameters
3. What is the output of the following Ruby code?
h = Hash.new { |hash, key| hash[key] = [] }
h[:a] << 1
h[:b] << 2
p h
medium
A. {:a=>[1], :b=>[2], :c=>[]}
B. {:a=>[1], :b=>[2], nil=>[]}
C. {:a=>[1], :b=>[2]}
D. {}
Solution
Step 1: Understand default block behavior
The hash is created with a block that assigns an empty array to missing keys when accessed.
Step 2: Modify arrays for keys :a and :b
Appending 1 to h[:a] creates :a => [1]. Similarly, appending 2 to h[:b] creates :b => [2].
Step 3: Print the hash
Only keys :a and :b have values. No other keys exist.
Final Answer:
{:a=>[1], :b=>[2]} -> Option C
Quick Check:
Default arrays assigned and modified correctly [OK]
Hint: Default block assigns new array per missing key [OK]
Common Mistakes:
Expecting keys not accessed to appear
Confusing default value with nil
Thinking default array is shared
4. Identify the error in this Ruby code that tries to set a default value of 0 for missing keys:
h = Hash.new
h.default = 0
puts h[:missing]
medium
A. h.default=0 sets default but does not affect missing keys
B. No error; code works and prints 0
C. h.default=0 should be h.default = 0 (space required)
D. Syntax error in setting default value
Solution
Step 1: Check default value assignment
Using h.default = 0 correctly sets the default value for missing keys after hash creation.
Step 2: Access missing key
Accessing h[:missing] returns 0 as expected, no error occurs.
Final Answer:
No error; code works and prints 0 -> Option B
Quick Check:
Setting default after creation works [OK]
Hint: Use h.default = value to set default after creation [OK]
Common Mistakes:
Thinking default= is a syntax error
Confusing default= with default_proc
Expecting missing keys to raise errors
5. You want a Ruby hash that counts how many times each word appears in an array. Which code correctly uses default values to do this?