Whats the Ruby Difference Between the 3 “P’s” Print, Puts, and P

Ahmed A.
4 min readSep 4, 2020

--

Printing to the command line is very useful, it can be used to test code, interact with a user, and many other useful reasons. While being useful, with having a few options to choose for to print to the terminal can get rather confusing which you should be using. We will take a look at the options available to us in Ruby and try to understand them. Knowing what is printed and what they return is important so that you do not get many annoying bugs in your program and have a less stressful time programming.

The methods we will be discussing will be:

  • Puts
  • Print
  • P
  • PP

As you will see most of these methods are similar with slight differences and can be useful if used correctly.

Puts

Puts is the first method we will discuss, and most likely the one you will use the most. When we call Puts whatever you pass to it is printed to the command line, and is also followed by a newline. (shown below)

Puts will turn whatever is passed into it into a string by making use of the .to_s method. This will make any elements you make nil print out as an empty string. (shown below)

Another important feature of puts is that every individual element will print on a new line as you can see in the array above. Also as seen in both the examples when using Puts the return value is always nil.

Print

Now we will look at Print method, while Print is almost indistinguishable from Puts there are some subtle differences. For example using Print will turn the argument passed into it to a string by using the .to_s method, but it will not add a new line after. (shown below)

You can also see in the above example that when we try and Print an array the whole array will print unlike Puts where each individual element of the array was printed on the new line. But similarly to Puts, Print will also return a value of nil.

P

Comparing the previous methods, Print and Puts, there are some serious differences between those and P. P will print out a more “raw” object, it does this by using the inspect method to convert the object to a string.

As you see in the above example when using P it will add a new line after printing out the object like Puts, it will also print out the whole array like Print. But unlike both Puts and Print it will return the object, with returning the object and also having the object be “raw” we can see that using P is very useful as a debugging tool.

PP

PP which is short for PrettyPrint is a special version of P, it has the same functionality as P but it tries to print larger hashes in an easier to read way. The example below will show you the difference between P and PP when printing a hash, and you can decide which you like better.

Using P to print a large hash
Using PP to print a large hash

At the end of the day you have a few options to choose from, and hopefully after reading this you will be able to have a better understanding of what will work best for you and your program while writing and debugging your code. Happy coding!

References:

Understanding The Differences Between Puts, Print & P

Printing things

Ruby Kernel version: 2.6.1

--

--