0Pricing
Ruby Academy · Lesson

Interpolation and Formatting

Build strings with values.

Interpolation and Formatting is a free Ruby Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

String Interpolation

Interpolation lets you embed Ruby expressions inside a double-quoted string using #{ }. The expression is evaluated and inserted as text.

name = "Ada"
puts "Hello, #{name}!"

Single vs Double Quotes

Interpolation only works in double-quoted strings. Single quotes treat #{ } as literal text.

x = 5
puts "value is #{x}"
puts 'value is #{x}'

Expressions Inside Interpolation

You can put any Ruby expression in #{ }, including math and method calls.

a = 4
b = 6
puts "Sum is #{a + b}"
puts "Upper: #{"ruby".upcase}"

Multiple Values

Combine several values into one readable sentence.

item = "book"
price = 12
puts "The #{item} costs #{price} dollars."

Concatenation vs Interpolation

Interpolation is usually cleaner than + because Ruby auto-converts values to strings. With + you must call to_s yourself.

age = 30
puts "Age: " + age.to_s
puts "Age: #{age}"

The format Method

format (alias sprintf) builds strings from a template. Use %s for strings and %d for integers.

result = format("%s scored %d", "Ada", 95)
puts result

Formatting Floats

Use %f for floats. Add a precision like %.2f to show two decimal places.

pi = 3.14159
puts format("Pi is %.2f", pi)
puts format("Pi is %.4f", pi)

Padding Numbers

Width specifiers align output. %5d right-aligns in 5 spaces; %-5d left-aligns; %05d pads with zeros.

puts format("[%5d]", 42)
puts format("[%-5d]", 42)
puts format("[%05d]", 42)

String % Operator

The % operator on a string is a shortcut for format. Pass a single value or an array of values.

puts "Hello, %s" % "World"
puts "%s is %d" % ["x", 10]

Alignment with ljust and rjust

ljust and rjust pad a string to a given width. center centers it. Great for tables.

puts "abc".ljust(8, ".")
puts "abc".rjust(8, ".")
puts "abc".center(9, "-")

Heredocs for Long Text

A heredoc writes multi-line text. The squiggly form <<~ strips leading indentation. Interpolation still works.

name = "Ada"
text = <<~MSG
  Dear #{name},
  Welcome aboard!
MSG
puts text

Quick Check

Check your formatting knowledge.

Recap

You now build strings cleanly:

  • #{ } interpolation in double quotes
  • format / % with %s, %d, %.2f
  • ljust, rjust, center for alignment
  • heredocs for long multi-line text

Next: searching and replacing inside strings.

user = "Ada"
puts format("User %s ready", user)

Frequently asked questions

Is the “Interpolation and Formatting” lesson free?

Yes — the full text of “Interpolation and Formatting” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.

What will I learn in “Interpolation and Formatting”?

Build strings with values. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ruby Academy?

No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Interpolation and Formatting” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ruby Academy lesson?

Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. String Basics and Methods
  2. Interpolation and Formatting
  3. Searching and Replacing
  4. Splitting and Joining
← Back to Ruby Academy