0
0
Rubyprogramming~10 mins

Accessing and setting values 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 value of key 'name' in the hash.

Ruby
person = {"name" => "Alice", "age" => 30}
puts person[[1]]
Drag options to blanks, or click blank then click option'
A"name"
B:name
C"age"
D:age
Attempts:
3 left
💡 Hint
Common Mistakes
Using symbol keys like :name when the hash uses string keys.
Using the wrong key like "age" instead of "name".
2fill in blank
medium

Complete the code to set the value of key :age to 25 in the hash.

Ruby
person = {name: "Bob", age: 20}
person[[1]] = 25
puts person[:age]
Drag options to blanks, or click blank then click option'
A"age"
B:age
C"name"
D:name
Attempts:
3 left
💡 Hint
Common Mistakes
Using string keys like "age" when the hash uses symbols.
Trying to set the value with the wrong key.
3fill in blank
hard

Fix the error in the code to correctly access the value of key :city.

Ruby
location = {city: "Paris", country: "France"}
puts location[[1]]
Drag options to blanks, or click blank then click option'
A"city"
B:country
C"country"
D:city
Attempts:
3 left
💡 Hint
Common Mistakes
Using string keys like "city" instead of symbol keys.
Using the wrong key like :country.
4fill in blank
hard

Fill both blanks to create a hash with keys as strings and values as integers.

Ruby
scores = {"math" => [1], "science" => [2]
puts scores
Drag options to blanks, or click blank then click option'
A90
B"90"
C85
D"85"
Attempts:
3 left
💡 Hint
Common Mistakes
Using string values like "90" instead of integer 90.
Mixing string and integer types for values.
5fill in blank
hard

Fill all three blanks to create a hash with symbol keys and update one value.

Ruby
person = {name: [1], age: [2], city: [3]
person[:age] = 40
puts person
Drag options to blanks, or click blank then click option'
A"John"
B30
C"New York"
D"30"
Attempts:
3 left
💡 Hint
Common Mistakes
Using string "30" instead of integer 30 for age.
Not using quotes for string values.