0
0
Rubyprogramming~15 mins

Heredoc syntax for multiline strings in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Heredoc Syntax for Multiline Strings in Ruby
📖 Scenario: Imagine you are writing a simple Ruby program that needs to store and display a multiline message, like a letter or a poem. Using heredoc syntax makes it easy to keep the message readable and neat in your code.
🎯 Goal: You will create a multiline string using Ruby's heredoc syntax and then print it to the screen.
📋 What You'll Learn
Create a multiline string using heredoc syntax with the exact content provided.
Assign the multiline string to a variable named message.
Print the message variable to display the full multiline text.
💡 Why This Matters
🌍 Real World
Heredoc syntax is useful when you need to include large blocks of text in your Ruby programs, such as email templates, SQL queries, or formatted messages.
💼 Career
Many Ruby developers use heredoc syntax to keep code clean and readable when working with multiline strings in web development, scripting, or automation tasks.
Progress0 / 4 steps
1
Create a multiline string using heredoc syntax
Create a variable called message and assign it a multiline string using heredoc syntax. The string should exactly contain these three lines:
Hello, friend!
Welcome to Ruby programming.
Enjoy learning heredoc syntax.
Ruby
Need a hint?

Use <<~TEXT to start the heredoc and TEXT alone on a line to end it.

2
Add a configuration variable for a greeting prefix
Create a variable called greeting and set it to the string "*** Message Start ***\n". This will be used as a prefix before the message.
Ruby
Need a hint?

Remember to include the newline character \n at the end of the greeting string.

3
Combine the greeting and message
Create a new variable called full_message that combines the greeting variable and the message variable using string concatenation.
Ruby
Need a hint?

Use the + operator to join the two strings.

4
Print the full multiline message
Write a puts statement to print the full_message variable to the screen.
Ruby
Need a hint?

Use puts full_message to display the combined message.