Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a hash with a symbol key.
Ruby
person = { [1] => 'Alice' } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around the key when a symbol is needed.
✗ Incorrect
In Ruby, symbol keys are written with a colon before the name, like :name.
2fill in blank
mediumComplete the code to access the value using a string key.
Ruby
person = { 'name' => 'Bob' }
puts person[[1]] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a symbol to access a string key.
✗ Incorrect
When the key is a string, you must use the exact string to access the value, including quotes.
3fill in blank
hardFix the error in accessing the hash value with a symbol key.
Ruby
person = { name: 'Carol' }
puts person[[1]] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string to access a symbol key.
✗ Incorrect
The hash uses a symbol key :name, so you must use the symbol to access it.
4fill in blank
hardFill both blanks to create a hash with a string key and access its value.
Ruby
data = { [1] => 42 }
puts data[[2]] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing symbol and string keys.
✗ Incorrect
The hash key is a string 'answer', so you must use the same string to access it.
5fill in blank
hardFill all three blanks to create a hash with symbol keys and print the value for :city.
Ruby
location = { [1] => 'New York', [2] => 'USA' }
puts location[[3]] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using string keys to access symbol keys.
✗ Incorrect
The hash uses symbol keys :city and :country. To print the city, use :city as the key.