Search⌘ K
AI Features

Exercise 2: Nested Hash

Explore how to work with nested hashes in Ruby by retrieving keys from inner hashes. This exercise helps you understand hash syntax and key access within the built-in Hash class.

We'll cover the following...

Problem statement

We have a nested hash called dictionary that contains other hashes as stored values. We also have a symbol called key. Your task is to return the keys stored in the hash that’s stored against the key.

Example

dictionary = {  :de => { :one => 'eins', :two => 'zwei', :three => 'drei' },  :en => { :one => 'one', :two => 'two', :three => 'three' },  :es => { :one => 'uno', :two => 'dos', :three => 'tres' }}

Input key = :en

Expected Output: [:one, :two, :three]

Try it yourself

Ruby
def return_hash(dictionary, key)
result = []
# Start your code here
return result
end