📖 Scenario: Imagine you have information about a library's book collection stored in nested hashes. You want to find details about specific books easily without checking each level manually.
🎯 Goal: You will create a nested hash representing books, set up keys to access, use the dig method to get nested values safely, and print the result.
📋 What You'll Learn
Create a nested hash called library with specific book details
Create an array called keys with the path to a nested value
Use the dig method on library with *keys to get the nested value
Print the result using puts
💡 Why This Matters
🌍 Real World
Nested data structures like hashes inside hashes are common in real applications such as configuration files, JSON data, or API responses. The <code>dig</code> method helps access deep values without errors.
💼 Career
Understanding how to safely access nested data is important for backend developers, data engineers, and anyone working with complex data formats.
Progress0 / 4 steps
1
Create the nested hash library
Create a nested hash called library with this exact structure: { 'fiction' => { '1984' => { 'author' => 'George Orwell', 'year' => 1949 } } }
Ruby
Hint
Use curly braces {} to create hashes inside hashes.
2
Create the array keys for nested access
Create an array called keys with these exact strings: 'fiction', '1984', 'author'
Ruby
Hint
Use square brackets [] to create an array with strings.
3
Use dig to get the nested author
Use the dig method on library with *keys to get the nested author name and store it in a variable called author_name
Ruby
Hint
Use dig with the splat operator * to pass array elements as arguments.
4
Print the author name
Print the variable author_name using puts to display the author's name
Ruby
Hint
Use puts author_name to print the value.
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]