String Interpolation
In this page:
String Interpolation
Ruby lets you embed expressions directly inside a double-quoted string with #{expression}, which evaluates the expression and inserts its string form. This only works in double-quoted strings, not single-quoted ones. Interpolation is the idiomatic Ruby way to build strings, generally preferred over concatenation with +.
Warning: String interpolation only works inside double quotes -- 'Hello #{name}' with single quotes prints the literal text, not the interpolated value.
Example: String Interpolation
name = "Ruby"
version = 3.2
puts "Hello, #{name}! Version: #{version}"
puts "2 + 2 = #{2 + 2}"
puts 'No interpolation here: #{name}'
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: