0
0
Rubyprogramming~10 mins

Symbol keys vs string keys decision in Ruby - Interactive Practice

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

Complete the code to create a hash with a symbol key.

Ruby
person = { [1] => 'Alice' }
Drag options to blanks, or click blank then click option'
A:name
B'name'
C"name"
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around the key when a symbol is needed.
2fill in blank
medium

Complete 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'
A:name
Bname
C'name'
D"name"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a symbol to access a string key.
3fill in blank
hard

Fix 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'
A:name
Bname
C"name"
D'name'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string to access a symbol key.
4fill in blank
hard

Fill 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'
A'answer'
B:answer
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing symbol and string keys.
5fill in blank
hard

Fill 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'
A:city
B:country
Attempts:
3 left
💡 Hint
Common Mistakes
Using string keys to access symbol keys.