0
0
Rubyprogramming~10 mins

Dig method for nested access in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Afetch
Bdig
Cget
Daccess
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.
2fill in blank
medium

Complete 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'
Afetch
Bget
Cdig
Dfind
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.
3fill in blank
hard

Fix 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'
Adig
Bfetch
Cget
Daccess
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.
4fill in blank
hard

Fill 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'
Alanguage
Bname
Ccode
Dversion
Attempts:
3 left
💡 Hint
Common Mistakes
Using keys that do not match the dig arguments.
Swapping the keys order.
5fill in blank
hard

Fill 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'
Adig
B:display
C:brightness
D:contrast
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect keys that do not exist in the hash.
Using fetch or square brackets instead of dig.