Ascent

p() vs. puts() vs. print() in Rails

Use p.

Rails has three printing methods, two are common, one is less so.

  1. print prints the string (to_s) version of what is passed to it.
  • puts works like print but adds a newline \n.
  • p prints the .inspect version of what is passed to it, along with the trailing newline.

Personally, i type some form of:

puts some_object.inspect

multiple times a day. No more. This statement functions identically to:

p some_object

11 characters less, and typos are nearly impossible. Type less, code more.

Get the latest posts delivered right to your inbox.
Author image
Written by Ben
Ben is the co-founder of Skyward. He has spent the last 10 years building products and working with startups.