What is "p" in Ruby?

I'm sure it's a silly question to those who know, but I can't find an explanation of what it does or what it is.

CSV.open('data.csv', 'r') do |row|
  p row
end

What does "p row" do?


p() is a Kernel method

It writes obj.inspect to the standard output.

Because Object mixes in the Kernel module, the p() method is available everywhere.

It's common, btw, to use it in poetry mode, meaning that the parens are dropped. The CSV snippet can be written like...

CSV.open 'data.csv', 'r' do |row|
  p row
end

It's documented here with the rest of the Kernel module.


Kernel#p is the little debugging brother of Kernel#puts: it more or less works exactly like it, but it converts its arguments using #inspect instead of #to_s.

The reason why it has such a cryptic name is so that you can quickly throw it into an expression and take it out again when debugging. (I guess it's a lot less useful now that Ruby is getting better and better "proper" debugging support.)

Some alternatives to Kernel#p are Kernel#pp (pretty print) from the pp standard library and Kernel#y (YAML) from the yaml standard library.