The dig method safely accesses nested elements inside hashes or arrays without raising errors if a key or index is missing. It returns nil instead.
Click to reveal answer
beginner
How do you use dig to get a value from a nested hash?
You call dig on the hash and pass the keys in order. For example, hash.dig(:key1, :key2) returns the value inside :key2 nested in :key1.
Click to reveal answer
beginner
What happens if a key or index does not exist when using dig?
Instead of raising an error, dig returns nil. This helps avoid crashes when accessing deep nested data.
Click to reveal answer
intermediate
Can dig be used with arrays as well as hashes?
Yes! You can use dig to access nested arrays by passing indexes along with hash keys. For example, array.dig(0, :key) works if the first element is a hash.
Click to reveal answer
intermediate
Why is dig better than chaining multiple [] calls?
Because chaining [] calls can raise errors if any key or index is missing. dig safely returns nil without crashing, making code cleaner and safer.
Click to reveal answer
What does hash.dig(:a, :b) return if :a exists but :b does not?
ARaises an error
Bnil
CReturns the whole hash
DReturns false
✗ Incorrect
dig returns nil if any key in the chain is missing.
Which of these is a correct use of dig to access nested data?
Ahash.dig(:key1, :key2)
Bhash[:key1][:key2]
Chash.get(:key1, :key2)
Dhash.fetch(:key1).fetch(:key2)
✗ Incorrect
dig takes multiple keys and safely returns nested values.
Can dig be used to access elements inside arrays?
AOnly if arrays are converted to hashes
BNo, only hashes
COnly with strings
DYes, by passing indexes as arguments
✗ Incorrect
dig works with arrays by passing numeric indexes.
What is the main advantage of using dig over chaining [] calls?
AIt prints the nested value
BIt runs faster
CIt avoids errors if keys or indexes are missing
DIt modifies the original data
✗ Incorrect
dig safely returns nil instead of raising errors.
What will [{a: 1}].dig(0, :a) return?
A1
Bnil
CRaises an error
DAn array
✗ Incorrect
The first element is a hash with key :a, so dig returns 1.
Explain how the dig method helps when accessing nested hashes or arrays.
Think about what happens if a key or index is missing.
You got /4 concepts.
Describe a situation where using dig is better than chaining multiple [] calls.
Consider what happens if one part of the chain is missing.
You got /4 concepts.
Practice
(1/5)
1. What does the Ruby dig method do when used on a nested hash or array?
easy
A. It safely accesses nested elements and returns nil if any key or index is missing.
B. It deletes the nested element at the given keys or indexes.
C. It converts the nested hash or array into a flat array.
D. It raises an error if any nested key or index is missing.
Solution
Step 1: Understand the purpose of dig
The dig method is designed to access nested data structures like hashes or arrays without raising errors if a key or index is missing.
Step 2: Behavior on missing keys or indexes
If any part of the nested path does not exist, dig returns nil instead of raising an error.
Final Answer:
It safely accesses nested elements and returns nil if any key or index is missing. -> Option A
Quick Check:
dig returns nil on missing keys/indexes [OK]
Hint: Remember: dig returns nil if path missing, no error [OK]
Common Mistakes:
Thinking dig deletes elements
Expecting dig to flatten nested structures
Assuming dig raises errors on missing keys
2. Which of the following is the correct syntax to use dig on a hash h = {a: {b: 2}} to get the value 2?
easy
A. h.dig(:a).dig(b)
B. h.dig[:a][:b]
C. h[:a].dig(b)
D. h.dig(:a, :b)
Solution
Step 1: Check the correct method call syntax
The dig method is called with arguments representing the nested keys or indexes, separated by commas inside parentheses.
Step 2: Validate each option
h.dig(:a, :b) uses h.dig(:a, :b), which is the correct syntax. h.dig[:a][:b] uses square brackets incorrectly. h[:a].dig(b) uses b without the leading colon :, causing NameError. h.dig(:a).dig(b) chains dig calls unnecessarily and uses b without :, causing NameError.
Final Answer:
h.dig(:a, :b) -> Option D
Quick Check:
dig called with keys as arguments [OK]
Hint: Use parentheses and commas for dig keys: dig(:a, :b) [OK]