Ruby p vs puts vs print
Ruby has three useful methods for printing information to the command line:
irb and follow along!
Kernel#print is the simplest of printing methods.
to_s on the
object and spits it out to
print does not append a new line.
New lines must be added manually when using
Kernel#puts is probably the most common printing methods known to Rubyists.
puts is similar to
print – calling
to_s – but adds a newline to the output.
Kernel#p is less well known than
It is similar to
puts in that it adds a newline, but rather than calling
p can be more useful for debugging. As
to_s, you can’t be exactly sure of what class an object is.
p we get a better indicator of what we’re actually looking at.