How to add new line in Ruby?
Last Updated :
23 Jul, 2025
In Ruby, newlines (line breaks) play a crucial role in formatting and separating your code. Whether you're crafting clear error messages or building readable output, knowing how to add newlines is essential.
Here's a breakdown of various methods you can use:
1. The Almighty `puts`:
The built-in `puts` method is your go-to tool for printing data to the console, and it automatically adds a new line at the end by default. This makes it ideal for displaying simple messages:
Ruby
puts "Hello, world!" # Prints "Hello, world!" followed by a newline
2. String Interpolation with `\n`:
For more control over output formatting, you can use string interpolation within double-quoted strings (`"`). The `\n` escape sequence represents a newline character, allowing you to embed newlines within your strings:
Ruby
name = "Alice"
message = "Welcome, #{name}\nHave a wonderful day!"
puts message # Prints:
# Welcome, Alice
# Have a wonderful day!
3. The Multiline String Heredoc (`<<-HEREDOC`):
When you need to create multiline strings, especially for displaying formatted text, Ruby offers the heredoc syntax. It lets you define a string literal that spans multiple lines, preserving the line breaks:
Ruby
prompt = <<-HEREDOC
What is your name?
>
HEREDOC
puts prompt # Prints:
# What is your name?
# >
4. The Multiline String Slash (`/`) Syntax:
Similar to heredoc, the slash syntax (`/`) allows you to define a multiline string. However, it treats leading whitespace consistently across lines, which can be useful for code blocks within strings:
Ruby
code_snippet = "/
puts 'Hello from the multiline string!'
/"
puts code_snippet # Prints:
#
# puts 'Hello from the multiline string!'
# /
Choosing the Right Method:
- Use `puts` for simple output with automatic newlines.
- Employ string interpolation with `\n` to insert newlines within strings for controlled formatting.
- Opt for heredoc for multiline text displays where you want to preserve line breaks and indentation as written.
- Choose the slash syntax for multiline strings where consistent leading whitespace management is desired, especially for code snippets embedded within strings.
Conclusion:
By mastering these techniques, you'll be able to effectively add newlines and manage your output in Ruby, creating clear, well-formatted, and user-friendly code.
Similar Reads
How to add new line Scala? In this article, we will learn how to add a new line in Scala. 1. Using the newline character (\n):This is the most straightforward approach. The \n character represents a newline and will be interpreted as such when the string is printed. Scala // Creating object object GfG { // Main method def mai
2 min read
Add New Line to Text File in R In this article, we are going to add a new line to the Text file using the R Programming language. It can be done in different ways: Using write() function.Using lapply() function.Using cat() function. Method 1: Adding text using write() function. Now we are going to select the line which is going t
3 min read
How to install Ruby on Linux? Prerequisite: Ruby Programming Language Before we start with the installation of Ruby on Linux, we must have first-hand knowledge of what Ruby is?. Ruby is a pure Object-Oriented language developed by Yukihiro Matsumoto (also known as Matz in the Ruby community) in the mid-1990s in Japan. Everything
2 min read
How to create a New Line in PHP? When working with text output in PHP, like creating a webpage, sending an email, or saving to a file, it's important to know how to add a new line. Adding a new line helps break up the text, makes it easier to read, and keeps it looking right whether it's shown in a browser, a terminal, or a file.Ho
2 min read
How to Create a Linked List in Ruby? A linked list is a form of linear data structure whereby the elements are not positioned closely together in memory. Each element here instead points to the subsequent one, thus leading to a chain-like arrangement. Creating a linked list in Ruby entails defining an information structure where each n
5 min read
How to Add New Line in Dictionary in Python Dictionaries are key-value stores that do not inherently support formatting like new lines within their structure. However, when dealing with strings as dictionary values or when outputting the dictionary in a specific way, we can introduce new lines effectively. Let's explore various methods to add
3 min read