String Interpolation (Ruby)

String interpolation embeds the result of a Ruby expression inside a double-quoted string using #{...}. It builds strings from variables and code without manual concatenation.

Ruby

Inside double-quoted strings (and heredocs), #{expression} is evaluated and its to_s result is inserted: "Hello, #{name}!". Any expression works — method calls, arithmetic, even multi-statement code — making it far cleaner than "Hello, " + name + "!".

Interpolation only works in double-quoted strings, not single-quoted ones, where #{...} is treated as literal text. Single quotes are for when you want exactly the characters typed with no processing.

Because #{} calls to_s automatically, you can interpolate any object, including arrays and hashes. This is the standard, idiomatic way to construct dynamic strings, log messages, and output in Ruby.

Syntax

"text #{expression} more text"

Examples

Ruby

name = "Ada"
items = [1, 2, 3]
puts "#{name} has #{items.size} items, sum #{items.sum}"

Output:

Ada has 3 items, sum 6

Learn it in a lesson

See also

Back to the Dictionary