0
0
Rubyprogramming~10 mins

String interpolation with #{} 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 insert the variable name inside the string using interpolation.

Ruby
name = "Alice"
message = "Hello, [1]!"
puts message
Drag options to blanks, or click blank then click option'
A"${name}"
B"name"
C'#{name}'
D"#{name}"
Attempts:
3 left
💡 Hint
Common Mistakes
Using single quotes instead of double quotes for interpolation.
Putting the variable name as a string without #{ }.
Using ${} which is not Ruby syntax.
2fill in blank
medium

Complete the code to include the result of 5 + 3 inside the string using interpolation.

Ruby
result = "The sum is [1]"
puts result
Drag options to blanks, or click blank then click option'
A"#{5 + 3}"
B"5 + 3"
C'#{5 + 3}'
D"${5 + 3}"
Attempts:
3 left
💡 Hint
Common Mistakes
Using single quotes which do not interpolate.
Putting the expression as a string without #{ }.
Using ${} which is not Ruby syntax.
3fill in blank
hard

Fix the error in the code to correctly interpolate the variable age inside the string.

Ruby
age = 30
puts "I am [1] years old."
Drag options to blanks, or click blank then click option'
A"age"
B#{age}
Cage
D'#{age}'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the variable name without #{ }.
Using single quotes which do not interpolate.
Putting the variable name as a string.
4fill in blank
hard

Fill both blanks to create a greeting that includes the first_name and last_name variables using interpolation.

Ruby
first_name = "John"
last_name = "Doe"
greeting = "Hello, [1] [2]!"
puts greeting
Drag options to blanks, or click blank then click option'
A#{first_name}
Bfirst_name
C#{last_name}
Dlast_name
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names without #{ }.
Using single quotes which do not interpolate.
5fill in blank
hard

Fill all three blanks to create a sentence that includes the city, temperature, and unit variables using interpolation.

Ruby
city = "Paris"
temperature = 22
unit = "C"
weather = "The temperature in [1] is [2]°[3]."
puts weather
Drag options to blanks, or click blank then click option'
A#{city}
B#{temperature}
C#{unit}
Dtemperature
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names without #{ }.
Using single quotes which do not interpolate.
Using plain variable names without interpolation.