Skip to content
Justice Tech School

Heredocs and Formatting

A heredoc holds multi line text. format and % build strings with placeholders such as %d, %f, %s and %05.2f.

Example
name = "Ada"
text = <<~TXT
  Dear #{name},
    Welcome!
TXT
puts text
puts format("%05d|%.2f|%-5s|", 42, 3.14159, "hi")
puts "%s is %d" % ["Ada", 21]

Result

Dear Ada,
  Welcome!
00042|3.14|hi   |
Ada is 21

Exercise

Format a float.

format("%.2f", )

Quiz Test

Which prefix removes heredoc indentation?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.