×

ruby · level 2 · unit 3 · lesson 4

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

A holds many lines of text in one string.

Every line of text ends with an invisible character. Inside double quotes you can write it as \n, so "one\ntwo" is two lines.

For longer text, use a heredoc. Start with <<~ and a word in capitals, write your lines, then put the same word on its own line to close it. The ~ removes the spaces at the start of each line.

.lines cuts a string into an array of lines, each keeping its newline. .each_line loops over them, and .chomp bites each newline off.

puts "Ready\nSteady\nGo!"
poem = <<~TEXT
  Roses are red
  Violets are blue
TEXT
p poem.lines.length
poem.each_line do |line|
  puts line.chomp.upcase
end

Pick a closing word that never appears in your text, like TEXT or POEM.