p
is used to inspect a variable as a debug aide. It works printing the output of the method #inspect
. For example p foo
will output the content of foo.inspect
.
Sometimes you need to debug complex variables or nested variables. In this case p
will output a long line that is hard to understand. Instead, pp
will put try to arrange the content of the variable so that it is easier to understand, for example indenting nested arrays or using one line for each instance variable of a complex object. pp
does this calling the #pretty_inspect
method (the pp
library adds #pretty_inspect
methods to many classes such as String
, Array
or Struct
).
To remember: p
= print, pp
= pretty print.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…