Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to access the nested value using dig.
Ruby
data = { user: { profile: { name: 'Alice' } } }
puts data.[1](:user, :profile, :name) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using fetch instead of dig causes errors if keys are missing.
Using get or access which are not Ruby Hash methods.
✗ Incorrect
The dig method allows you to safely access nested hash values by passing the keys in order.
2fill in blank
mediumComplete the code to safely get the city from nested data using dig.
Ruby
address = { location: { city: 'Paris', country: 'France' } }
city = address.[1](:location, :city)
puts city Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using fetch which raises error if key is missing.
Using get or find which are not valid for nested hash access.
✗ Incorrect
dig safely returns the nested value or nil if any key is missing.
3fill in blank
hardFix the error in the code to correctly use dig for nested access.
Ruby
profile = { info: { age: 30 } }
age = profile.[1][:info, :age]
puts age Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets instead of parentheses with dig.
Using fetch or get which do not support multiple nested keys.
✗ Incorrect
The dig method should be called with parentheses and keys as arguments, not with square brackets.
4fill in blank
hardFill both blanks to create a nested hash and access a value using dig.
Ruby
data = { [1]: { [2]: 'Ruby' } }
puts data.dig(:language, :name) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using keys that do not match the dig arguments.
Swapping the keys order.
✗ Incorrect
The keys :language and :name match the dig arguments to access the value 'Ruby'.
5fill in blank
hardFill all three blanks to use dig to access a deeply nested value.
Ruby
config = { settings: { display: { brightness: 75 } } }
value = config.[1](:settings, [2], [3])
puts value Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect keys that do not exist in the hash.
Using fetch or square brackets instead of dig.
✗ Incorrect
Use dig with keys :settings, :display, and :brightness to get the value 75.