Bird
0
0

Why does Ruby require #{} for interpolation inside strings instead of just using variable names directly?

hard📝 Conceptual Q10 of 15
Ruby - Variables and Data Types
Why does Ruby require #{} for interpolation inside strings instead of just using variable names directly?
ABecause Ruby does not support variables inside strings
BTo clearly separate Ruby code from string text and allow expressions
CTo make strings immutable
DTo prevent any code execution inside strings
Step-by-Step Solution
Solution:
  1. Step 1: Understand string and code separation

    Ruby strings are text, so #{ } marks where Ruby code should be evaluated and inserted.
  2. Step 2: Explain why direct variable names are not enough

    Without #{ }, Ruby would not know where code ends and text begins, and it allows any Ruby expression, not just variables.
  3. Final Answer:

    To clearly separate Ruby code from string text and allow expressions -> Option B
  4. Quick Check:

    #{ } marks Ruby code inside strings [OK]
Quick Trick: #{ } tells Ruby where to run code inside strings [OK]
Common Mistakes:
MISTAKES
  • Thinking variables auto-expand without #{ }
  • Confusing immutability with interpolation syntax
  • Believing interpolation disables code execution

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes