• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Ruby / How to Print With Newline in Ruby

How to Print With Newline in Ruby

October 14, 2022 Leave a Comment

To print with a newline in Ruby, we simply need to use the puts command instead of print. Let’s take a look at each below.

Here is how the puts command works.

number_one = "one"
number_two = "two"
number_three = "three"
number_four = "four"
number_five = "five"

puts number_one
puts number_two
puts number_three
puts number_four
puts number_five

#Output
one
two
three
four
five

Let’s see how this example would work if we used the print command.

number_one = "one"
number_two = "two"
number_three = "three"
number_four = "four"
number_five = "five"

print number_one
print number_two
print number_three
print number_four
print number_five

#Output
onetwothreefourfive

So as you can see, the puts command will return a newline after it prints, while the print command will print without a newline after it.

Let’s look at another way to print with a newline in Ruby.

Using “\n” to Print Newline in Ruby

We can also use "\n" to return a new line. So let’s take our print example from above, and use the newline code "\n" to get a newline between each print.

number_one = "one"
number_two = "two"
number_three = "three"
number_four = "four"
number_five = "five"

print number_one + "\n"
print number_two + "\n"
print number_three + "\n"
print number_four + "\n"
print number_five + "\n"

#Output
one
two
three
four
five

Hopefully this article has been helpful for you to learn how to use Ruby to print with newline.

Other Articles You'll Also Like:

  • 1.  Ruby puts vs print
  • 2.  Ruby has_value Method – Check if Hash Has Value
  • 3.  Using Ruby to Capitalize First Letter of Each Word
  • 4.  Using Ruby to Get the Length of Array
  • 5.  Ruby include Method – Check if Item in Array
  • 6.  Using Ruby to Print to Console
  • 7.  Ruby String Comparison
  • 8.  Using Ruby to Convert String to Boolean
  • 9.  Using Ruby to Get the Length of a String
  • 10.  Ruby Infinite Loop

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy