0
0
Rubyprogramming~10 mins

Heredoc syntax for multiline strings in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a multiline string using heredoc syntax.

Ruby
text = <<[1]
This is a multiline string.
It spans multiple lines.
[1]
Drag options to blanks, or click blank then click option'
A'END'
B"END"
CEND
Dend
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around the delimiter which changes the string behavior.
Mismatching the starting and ending delimiters.
2fill in blank
medium

Complete the code to create a heredoc string that preserves indentation.

Ruby
text = <<~[1]
  Line one
  Line two
[1]
Drag options to blanks, or click blank then click option'
AHEREDOC
BEND
CDOC
DTEXT
Attempts:
3 left
💡 Hint
Common Mistakes
Using different delimiter names at start and end.
Not using the tilde (~) to enable indentation stripping.
3fill in blank
hard

Fix the error in the heredoc syntax to correctly assign a multiline string.

Ruby
message = <<[1]
Hello,
This is a test.
[1]
Drag options to blanks, or click blank then click option'
AMSG
B'MSG'
C"MSG"
Dmsg
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around the delimiter.
Using lowercase delimiter which is uncommon and may cause confusion.
4fill in blank
hard

Fill both blanks to create a heredoc string with interpolation enabled.

Ruby
name = "Alice"
text = <<[1]
Hello, #{name}!
[2]
Drag options to blanks, or click blank then click option'
AEND
B"END"
C'END'
Dend
Attempts:
3 left
💡 Hint
Common Mistakes
Quoting the delimiter which disables interpolation.
Mismatching the delimiters.
5fill in blank
hard

Fill all three blanks to create a heredoc string that disables interpolation and preserves indentation.

Ruby
name = "Bob"
text = <<~[1][2]
Hello, #{name}!
  Indented line.
[3]
Drag options to blanks, or click blank then click option'
A~
B'
CEND
D"
Attempts:
3 left
💡 Hint
Common Mistakes
Using double quotes which enable interpolation.
Omitting the tilde which keeps indentation.