Bird
0
0

Which of the following is the correct syntax for string interpolation in Ruby?

easy📝 Syntax Q3 of 15
Ruby - Variables and Data Types
Which of the following is the correct syntax for string interpolation in Ruby?
A'Hello, #{name}!'
B"Hello, $name!"
C"Hello, #{name}!"
D"Hello, ${name}!"
Step-by-Step Solution
Solution:
  1. Step 1: Recall Ruby interpolation syntax

    Ruby uses #{expression} inside double-quoted strings for interpolation.
  2. Step 2: Evaluate each option

    "Hello, #{name}!" uses correct syntax. "Hello, $name!" uses $ which is not for interpolation. 'Hello, #{name}!' uses single quotes, so no interpolation. "Hello, ${name}!" uses ${} which is not Ruby syntax.
  3. Final Answer:

    "Hello, #{name}!" -> Option C
  4. Quick Check:

    Correct interpolation syntax = #{ } inside double quotes [OK]
Quick Trick: Always use #{ } inside double quotes for interpolation [OK]
Common Mistakes:
  • Using ${} like other languages
  • Using $variable instead of #{variable}
  • Using single quotes expecting interpolation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes