puts vs print vs p in Ruby

In this article we are going to read about the differences between puts, print and p methods in Ruby that are used to output information.

Table of Contents

  • Introduction to various methods to output information in Ruby
  • Puts vs Print
  • Method P in Ruby
  • Puts vs P in Ruby
  • Parting Words

Introduction to various methods to output information in Ruby

As a beginner in Ruby, you will be practicing writing a lot of code and would have printed/output a lot of information.

While in most of the cases you will be using "puts" for this purpose there are other ways to output information in ruby too. In this article, we are going to talk about them.

Puts vs Print

Print() is a good choice for lines that are built up in many steps. We can avoid concatenating strings ourselves, as this method does not append a newline to the text. We can thus use many print statements, one after another, on a single line. But we must also print a newline manually.

Let's take a look at its use through a piece of code:

> print 1,2,3


One major difference between print and puts is that print does not append a newline automatically. Newlines must be manually added while using the print method.

> print 1,"\n",2,"\n",3,"\n"




In puts method, the newline is automatically appended.

> puts 1,2,3




Puts also treats array differently

puts [1,2]

print [1,2]

Method p in Ruby

Can there be a method that does more than just output information? The method p - an alternative to puts also helps in figuring out what a program is doing, why a certain piece of code is doing what it is doing, and what a certain error might be about.

Before going to "p", let's take a look at the method "inspect" which will help us understand p better.

Inspect method

The method inspect is available only in Ruby, and returns a string that is a representation of the object which is very close to the original code you wrote to create the object itself, which makes the inspect method useful in inspecting the objects.

Let's take a look at the code below to get more clarity:

> puts 5.inspect 


> puts "A string".inspect

"A string"

> puts [1, 2, 3].inspect

[1, 2, 3]

From the code, you can see that the output, on using the inspect method on the object (object here being 5), is the same as the object itself (5).

Using the inspect method for all statements might not seem like a big task in such small ruby codes, but when you think about 1000s of lines of code, writing puts something.inspect, might be quite the task.

To solve this exact problem, ruby has the method "p", which is defined as shown below.

def p(object)   
        puts object.inspect
        #p is defined as puts (something).inspect

So instead of writing puts something.inspect, everytime, you can use the method p to print the information.

Puts vs P

As already mentioned, in ruby the method p can come in handy when you are trying to figure out what a certain line of code does, what’s assigned to a variable, or what a method call returns. As it tells you exactly what you are looking at.

puts on the other hand tries to be smart, like, the output for numbers and strings that contain numbers is exactly the same when you use puts:

> puts 123

> puts "123"

If you are just beginning to learn ruby, this might easily confuse you, as from the output, it is often not clear whether the object that you are looking at is an array that contains numbers, or an array that contains strings, or just a long string that contains line breaks.

In such cases, the method p will be helpful. Let's take a look at the code below using p

> p 123

> p "123"

This will help you understand what the object is just by looking at the output.

Parting Words

In conclusion, print and puts method is useful in ruby when your end goal is to print information on a screen, like, this could be a command line tool that you write in order to make your own life easier at your job, and that helps automate some repetitive work. While the print method allows you to print information in the same line even multiple times, the puts method adds a new line at the end of the object.

On the other hand, p is useful when you are trying to understand what your code does, e.g. when you are trying to figure out a certain error.