0
0
Rubyprogramming~10 mins

Default values for missing keys in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Default values for missing keys
Create Hash
Access Key
Key Exists?
YesReturn Value
No
Return Default Value
When you try to get a value from a hash, Ruby checks if the key exists. If it does, it returns the value. If not, it returns a default value.
Execution Sample
Ruby
h = Hash.new("default")
h[:a] = 1
puts h[:a]
puts h[:b]
This code creates a hash with a default value 'default'. It sets :a to 1, then prints the value for :a and for a missing key :b.
Execution Table
StepActionKey AccessedKey Exists?Returned ValueNotes
1Create hash with default 'default'---Hash h created with default value 'default'
2Assign h[:a] = 1:aNo1Key :a added with value 1
3Access h[:a]:aYes1Returns stored value 1
4Access h[:b]:bNodefaultReturns default value because :b missing
💡 After accessing :b which is missing, hash returns default value 'default'
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4
h{}{:a=>1}{:a=>1}{:a=>1}
Returned Value-11"default"
Key Moments - 2 Insights
Why does h[:b] return 'default' instead of nil or error?
Because the hash was created with Hash.new("default"), Ruby returns the default value for missing keys as shown in execution_table row 4.
Does assigning a value to a key change the default value?
No, assigning h[:a] = 1 stores a value for :a but the default value remains unchanged, as seen in execution_table row 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is returned when accessing h[:a] at step 3?
A"default"
B1
Cnil
DError
💡 Hint
Check the 'Returned Value' column at step 3 in the execution_table.
At which step does the hash get a new key-value pair added?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' and 'Notes' columns to see when :a is assigned.
If the hash was created without a default value, what would h[:b] return at step 4?
Anil
B"default"
CError
D0
💡 Hint
Recall that without a default, missing keys return nil in Ruby hashes.
Concept Snapshot
Hash.new(default) creates a hash with a default value.
Accessing existing keys returns stored values.
Accessing missing keys returns the default value.
Assigning keys does not change the default.
Useful to avoid nil checks for missing keys.
Full Transcript
This visual trace shows how Ruby hashes with default values work. First, a hash h is created with a default value 'default'. Then, the key :a is assigned the value 1. When accessing h[:a], the stored value 1 is returned. When accessing a missing key h[:b], Ruby returns the default value 'default' instead of nil or error. The variable tracker shows the hash contents and returned values after each step. Key moments clarify why missing keys return the default and that assigning keys does not affect the default. The quiz tests understanding of returned values and when keys are added. This helps beginners see how default values simplify hash usage.