What is "p" in Ruby?
Asked Answered
A

6

46

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?

Aforesaid answered 18/11, 2009 at 19:3 Comment(0)
B
87

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.

Bogor answered 18/11, 2009 at 19:7 Comment(1)
The direct link into the docs for p - ruby-doc.org/core-1.8.7/Kernel.html#method-i-pDiskson
P
24

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.

Pantin answered 18/11, 2009 at 19:33 Comment(0)
T
5

Why not try it?

>> [1,2,3].each { |d| p d }
1
2
3
Tearful answered 18/11, 2009 at 19:5 Comment(2)
Actually it calls #inspect to its argument first, but close enough ;)Prostatitis
True. Removed my 'alias' comment since it's just plain wrong. Maybe more like 'wrapper'.Tearful
B
2

The other option for documentation that you already have on your system is the ri command. At any time you can type: ri p or if p is defined in a lot of places (which it is) for central commands you can try ri Kernel.p. Other good bets are Array.<whatever method> or String.<whatever method>.

If you end up installing a bunch of gems this will slow down a lot but you can look up the fastri gem which speeds up the lookup process incredibly.

Brewing answered 18/11, 2009 at 19:51 Comment(0)
M
1

Kernel#p is less well known than print and puts.

It is similar to puts in that it adds a newline, but rather than calling to_s, p calls inspect.

References

http://garethrees.co.uk/2013/05/04/p-vs-puts-vs-print-in-ruby/

Minelayer answered 17/7, 2014 at 4:37 Comment(0)
S
0

To understand the difference between p and puts, you must understand difference between to_s() and instance() methods.

to_s is used to get string representation of an object while instance is a more developer friendly version of to_s which gives contents of the objects as well.

class Dog
        def initialize(name, breed)
            @name = name
            @breed = breed
        end
        def to_s
            puts  "#@name's breed is #@breed."
        end
end

terra=Dog.new("Terra","Husky")
puts terra #Terra's breed is Husky.
p terra    #<Dog:0x00007fbde0932a88 @name="Terra", @breed="Husky">  
Syndesmosis answered 27/11, 2017 at 21:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.