0
0
Rubyprogramming~10 mins

String interpolation with #{} in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - String interpolation with #{}
Start with string containing #{expression}
Evaluate expression inside #{}
Convert expression result to string
Replace #{expression} with result string
Return final interpolated string
The program finds expressions inside #{}, evaluates them, converts to string, and replaces them in the original string.
Execution Sample
Ruby
name = "Alice"
age = 30
"Hello, #{name}. You are #{age} years old."
This code creates a string greeting that includes the values of variables name and age using interpolation.
Execution Table
StepActionExpression inside #{}Evaluated ResultString after replacement
1Evaluate first #{name}name"Alice""Hello, Alice. You are #{age} years old."
2Evaluate second #{age}age30"Hello, Alice. You are 30 years old."
3Final string ready--"Hello, Alice. You are 30 years old."
💡 All expressions inside #{} evaluated and replaced, final string returned.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
name"Alice""Alice""Alice""Alice"
age30303030
Key Moments - 2 Insights
Why does the expression inside #{} get evaluated before being inserted?
Because Ruby replaces #{expression} with the result of evaluating that expression, as shown in steps 1 and 2 of the execution_table.
Can we put any Ruby code inside #{}?
Yes, any valid Ruby expression can be inside #{}, and it will be evaluated and converted to string before insertion, as demonstrated by the evaluation steps.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the string after step 1?
A"Hello, #{name}. You are 30 years old."
B"Hello, Alice. You are #{age} years old."
C"Hello, Alice. You are 30 years old."
D"Hello, #{name}. You are #{age} years old."
💡 Hint
Check the 'String after replacement' column in row 1 of the execution_table.
At which step is the final string fully formed?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Step' column and see when the string has no more #{...} parts.
If the variable age was changed to 31 before interpolation, what would be the final string?
A"Hello, Alice. You are 31 years old."
B"Hello, Alice. You are 30 years old."
C"Hello, Alice. You are #{age} years old."
D"Hello, #{name}. You are 31 years old."
💡 Hint
Refer to variable_tracker to see how changing age affects the final string.
Concept Snapshot
String interpolation in Ruby uses #{expression} inside double quotes.
Ruby evaluates the expression, converts it to string, and inserts it.
Only works inside double-quoted strings or similar.
Useful for combining variables and text easily.
Example: "Hello, #{name}!" inserts the value of name.
Expressions can be any Ruby code inside #{}.
Full Transcript
String interpolation with #{ } in Ruby means putting Ruby code inside a string to be evaluated and replaced with its result. The program starts with a string containing #{expression}. It evaluates the expression inside the braces, converts the result to a string, and replaces the #{expression} with that string. This happens for each #{ } in the string. For example, if name is "Alice" and age is 30, then "Hello, #{name}. You are #{age} years old." becomes "Hello, Alice. You are 30 years old.". Variables keep their values during this process. This lets you build strings that include variable values or any Ruby expression easily and clearly.