← Back to Ruby Course | Chapter 1: Setup & Basics | Lesson 7 of 7

String Interpolation

In this page:

  1. String Interpolation

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

markup
name = "Ruby"
version = 3.2

puts "Hello, #{name}! Version: #{version}"
puts "2 + 2 = #{2 + 2}"
puts 'No interpolation here: #{name}'
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.