Skip to main content

How to Put Variables in a String in Ruby

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

In a double-quoted Ruby string, # followed by an expression in curly braces drops the expression's value into the text. Any expression works there, from a variable to a calculation; in single quotes, nothing is replaced.

Syntax​

Syntax
"Text #{<expression>} more text"
PartWhat it does
"..."A double-quoted string: interpolation works.
#{<expression>}Runs the expression and inserts its value, converted to text.
'...'A single-quoted string: printed exactly as written.

The Ruby literals documentation: "Double-quoted strings allow interpolation of other values". It adds that interpolation "may be disabled by escaping the "#" character or using single-quoted strings".

The Example​

The example builds an order summary from an item, a quantity and a price, calculating the total inside the string, then shows a single-quoted string for comparison.

Prerequisites​

  • Ruby. This guide uses Ruby 4.0.10.

Step 1: Write the Code​

Create a file named main.rb:

main.rb, which sets item to coffee, quantity to 2 and price to 3.5, puts a double-quoted sentence that interpolates the quantity, the item and the quantity times the price, and puts a single-quoted string with a placeholder.

main.rb
item = 'coffee'
quantity = 2
price = 3.5

puts "You ordered #{quantity} #{item}s for #{quantity * price} euros."
puts 'Single quotes: #{item}'

Step 2: Run It​

Terminal
ruby main.rb
Output
You ordered 2 coffees for 7.0 euros.
Single quotes: #{item}

A zsh terminal where ruby main.rb prints You ordered 2 coffees for 7.0 euros. and the single-quoted line with its placeholder unchanged.

  • #{quantity * price} ran the calculation and inserted 7.0: an Integer times a Float is a Float.
  • In single quotes, the placeholder stayed as written.

The command is the same in zsh, bash and PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Using single quotesThe placeholder is printed literallyUse double quotes
Joining a number with +, as in 'Total: ' + 7no implicit conversion of Integer into String (TypeError)Interpolate it: "Total: #{7}"
Putting a long calculation inside the bracesThe string becomes hard to readCalculate first, then interpolate the variable

Troubleshooting​

SymptomLikely causeFix
The placeholder appears in the outputThe string is single-quotedSwitch to double quotes
no implicit conversion of … into String+ between a string and another typeUse interpolation

FAQ​

Does interpolation call to_s?​

Yes. The inserted value is converted with to_s, which is why numbers and nil work; nil becomes an empty string.

How do I keep a placeholder literal in a double-quoted string?​

Escape the # with a backslash: "\#{item}" prints the placeholder itself instead of the value.

Conclusion​

Double-quoted strings replace each interpolation placeholder with the expression's value; single-quoted strings never do. Keep the expressions short and readable.

Additional Resources​