I’ve been trying to pick up Ruby and one of the things I was asking myself was the difference in the various ways you can output text.
- puts: displays text and adds a newline character
- print: displays text and doesn’t add a newline character (this means you must add newlines manually when using print)
- p: while it’s similar to puts in that it adds a newline, it calls
inspect
instead ofto_s
This is why p can be more useful for debugging since it shows what class an object belongs to.
More references for those interested
- https://matt.berther.io/2009/02/11/puts-vs-print-in-ruby/
- http://www.ruby-doc.org/core-1.8.7/Kernel.html#method-i-p