0
0
Rubyprogramming~10 mins

Why hashes are used everywhere in Ruby - Test Your Understanding

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 key 'name' and value 'Ruby'.

Ruby
person = { :name => [1] }
Drag options to blanks, or click blank then click option'
A"Ruby"
BRuby
C:Ruby
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around string values.
Using a symbol instead of a string for the value.
2fill in blank
medium

Complete the code to access the value of the key :age from the hash.

Ruby
person = { age: 25 }
age = person[[1]]
Drag options to blanks, or click blank then click option'
Aage
B"age"
C:age
D25
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string "age" instead of symbol :age.
Using the value 25 instead of the key.
3fill in blank
hard

Fix the error in the code to add a new key-value pair :city => 'Tokyo' to the hash.

Ruby
person = { name: "Ruby" }
person[1] = "Tokyo"
Drag options to blanks, or click blank then click option'
A.city
B[:city]
Ccity
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot notation which is invalid for hashes.
Using the key without brackets.
4fill in blank
hard

Fill both blanks to create a hash with keys :a and :b and values 1 and 2.

Ruby
numbers = { [1]: 1, [2]: 2 }
Drag options to blanks, or click blank then click option'
Aa
B1
Cb
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using numbers as keys instead of symbols.
Putting quotes around keys.
5fill in blank
hard

Fill all three blanks to create a hash with keys :x, :y and values 10, 20, and access the value of :y.

Ruby
coords = { [1]: [2], [3]: 20 }
y_value = coords[:y]
Drag options to blanks, or click blank then click option'
Ax
B10
Cy
D20
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up keys and values.
Using strings instead of symbols for keys.