Bird
Raised Fist0
Rubyprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the #{} syntax do inside a double-quoted string in Ruby?
easy
A. It inserts the value of the expression inside the braces into the string.
B. It comments out the code inside the braces.
C. It creates a new variable named after the expression inside the braces.
D. It escapes special characters inside the string.

Solution

  1. Step 1: Understand string interpolation syntax

    In Ruby, #{} inside double quotes allows inserting the value of Ruby expressions directly into the string.
  2. Step 2: Recognize the purpose of #{}

    This syntax evaluates the expression inside the braces and converts it to a string to include it in the larger string.
  3. Final Answer:

    It inserts the value of the expression inside the braces into the string. -> Option A
  4. Quick Check:

    #{expression} inserts value [OK]
Hint: Use #{ } inside double quotes to embed values [OK]
Common Mistakes:
  • Using #{ } inside single quotes expecting interpolation
  • Thinking #{ } comments out code
  • Confusing #{ } with variable declaration
2. Which of the following is the correct way to use string interpolation to insert the variable name into a string?
easy
A. "Hello, \#{name}!"
B. 'Hello, #{name}!'
C. "Hello, #{name}!"
D. "Hello, #name!"

Solution

  1. Step 1: Identify correct string quotes for interpolation

    Ruby only processes #{} interpolation inside double-quoted strings, not single-quoted.
  2. Step 2: Check syntax correctness

    "Hello, #{name}!" uses double quotes and #{name} correctly. 'Hello, #{name}!' uses single quotes, so no interpolation. "Hello, \#{name}!" escapes the #, so no interpolation. "Hello, #name!" misses braces, so no interpolation.
  3. Final Answer:

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

    Double quotes + #{variable} = interpolation [OK]
Hint: Use double quotes and #{variable} for interpolation [OK]
Common Mistakes:
  • Using single quotes which do not interpolate
  • Escaping # which disables interpolation
  • Omitting braces around variable
3. What is the output of the following Ruby code?
name = "Alice"
age = 30
puts "Name: #{name}, Age: #{age + 5}"
medium
A. Name: Alice, Age: age + 5
B. Name: Alice, Age: 30 + 5
C. Name: #{name}, Age: #{age + 5}
D. Name: Alice, Age: 35

Solution

  1. Step 1: Evaluate expressions inside #{}

    The variable name is "Alice" and age is 30. The expression age + 5 evaluates to 35.
  2. Step 2: Construct the output string

    The string becomes "Name: Alice, Age: 35" and is printed by puts.
  3. Final Answer:

    Name: Alice, Age: 35 -> Option D
  4. Quick Check:

    #{age + 5} calculates 35 [OK]
Hint: Expressions inside #{ } are evaluated before insertion [OK]
Common Mistakes:
  • Treating #{age + 5} as a string literal
  • Not evaluating arithmetic inside interpolation
  • Confusing output with raw code
4. Identify the error in this Ruby code snippet:
count = 10
puts "You have #{count messages}"
medium
A. Invalid expression inside #{}, spaces not allowed without operator
B. Missing operator inside #{}, should be #{count + ' messages'}
C. Syntax error due to missing closing brace
D. No error, code runs fine

Solution

  1. Step 1: Analyze the expression inside #{}

    The expression inside interpolation is count messages which is invalid syntax because Ruby expects a valid expression without spaces unless operators are used.
  2. Step 2: Understand correct usage

    To concatenate or combine, you need an operator like + or string interpolation with proper syntax, e.g., "#{count} messages".
  3. Final Answer:

    Invalid expression inside #{}, spaces not allowed without operator -> Option A
  4. Quick Check:

    #{expression} must be valid Ruby code [OK]
Hint: Expressions inside #{ } must be valid Ruby code without spaces [OK]
Common Mistakes:
  • Putting spaces without operators inside #{ }
  • Forgetting to close #{ } properly
  • Assuming interpolation works with invalid expressions
5. You want to create a greeting message that includes a user's name and the current year calculated dynamically. Which code correctly uses string interpolation to achieve this?
name = "Sam"
current_year = Time.now.year
# Choose the correct greeting message
hard
A. "Hello, #{name}! Welcome to current_year."
B. "Hello, #{name}! Welcome to #{current_year}."
C. 'Hello, #{name}! Welcome to #{current_year}.'
D. "Hello, name! Welcome to #{Time.now.year}."

Solution

  1. Step 1: Check interpolation of variables

    "Hello, #{name}! Welcome to #{current_year}." correctly uses double quotes and interpolates both name and current_year variables.
  2. Step 2: Verify dynamic year calculation

    "Hello, name! Welcome to #{Time.now.year}." uses Time.now.year inside interpolation but misses interpolating name properly (it's plain text). "Hello, #{name}! Welcome to current_year." treats current_year as plain text. 'Hello, #{name}! Welcome to #{current_year}.' uses single quotes, so no interpolation.
  3. Final Answer:

    "Hello, #{name}! Welcome to #{current_year}." -> Option B
  4. Quick Check:

    Double quotes + #{variable} for dynamic values [OK]
Hint: Use double quotes and #{variable} for dynamic strings [OK]
Common Mistakes:
  • Using single quotes which disable interpolation
  • Not interpolating variables properly
  • Confusing variable names with plain text
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard