Comparison between the p, puts, and print Methods in Ruby

Published: Sunday, May 22, 2022

Greetings, friends! In Ruby, there are multiple ways to output information to the screen:

Notice that each of these methods belong to the Kernel class.

The puts method

It's very common to use the puts method to output information to the screen or a console such as irb, the "interactive ruby" shell.

ruby
Copied! ⭐️
puts "Greetings, friends!"
# OUTPUT: Greetings, friends!
tip
In this tutorial, we will not use run.rb, since the print method does not work correctly. It is better to create Ruby scripts (with the .rb extension) to run code snippets in this tutorial instead.

The puts method actually prints a newline character after each value passed into it.

ruby
Copied! ⭐️
puts "hello", "world"

=begin OUTPUT:
hello
world
=end

The puts method will print each value in an array onto a new line as well.

ruby
Copied! ⭐️
puts ["pizza", "donuts", "potato"]

=begin OUTPUT:
pizza
donuts
potato
=end

It is important to note that the puts method calls an object's to_s (to string) method internally. We can prove this by creating our own class with a to_s method.

ruby
Copied! ⭐️
class Cat
  attr_reader :name

  def initialize(name)
    @name = name
  end

  def to_s
    "Cat: #{name}"
  end
end

cat = Cat.new("Whiskers")
puts cat # Cat: Whiskers

Another interesting detail about the puts method is that it returns nil.

ruby
Copied! ⭐️
def debug(value)
  puts value
end

debugValue = debug("hi") # this line will print "hi" but debugValue will equal nil
puts debugValue == nil # true

The p method

Let's now look at the p method and call it on an array.

ruby
Copied! ⭐️
p ["pizza", "donuts", "potato"]
# OUTPUT: ["pizza", "donuts", "potato"]

As mentioned in the previous article, the p method helps us print arrays out on a single line because it calls an object's inspect method instead of its to_s method. We can prove this by creating our own class with an inspect method.

ruby
Copied! ⭐️
class Cat
  attr_reader :name

  def initialize(name)
    @name = name
  end

  def inspect
    "Cat: #{name}"
  end
end

cat = Cat.new("Whiskers")
p cat # Cat: Whiskers

Keep in mind that the p method will still print a newline character after each value:

ruby
Copied! ⭐️
p "pizza", "donuts", "potato"

=begin
"pizza"
"donuts"
"potato"
=end

Unlike the puts method, the p method will return the value of the object passed into it and will not return nil.

ruby
Copied! ⭐️
def debug(value)
  p value
end

debugValue = debug("hi") # this line will print "hi" and debugValue will equal "hi"
puts debugValue == "hi" # true

The print method

The print method is very similar to the puts method, but it does not print a newline character. It will internally call an object's to_s method and return nil.

ruby
Copied! ⭐️
class Cat
  attr_reader :name

  def initialize(name)
    @name = name
  end

  def to_s
    "Cat: #{name}"
  end
end

cat = Cat.new("Whiskers")
print cat # Cat: Whiskers

We can prove that the print method returns nil, similar to the puts method:

ruby
Copied! ⭐️
def debug(value)
  print value
end

debugValue = debug("hi") # this line will print "hi" and debugValue will equal "hi"
puts "\n" # add new line ourselves
puts debugValue == nil # true

Conclusion

To summarize the differences between each of the three methods discussed in this tutorial:

  • The puts method calls an object's to_s method followed by a newline character. It will also return a value of nil.
  • The p method calls an object's inspect method followed by a newline character. It will return the value of the object passed into it.
  • The print method calls an object's to_s method but does not add a newline character. It will return a value of nil.

Resources