# \#p is to #inspect as #pp is to?

**URL:** <https://www.ruby-forum.com/t/p-is-to-inspect-as-pp-is-to/121708>\
**Category:** Ruby\
**Created:** [November 30, 2007, 4:32am UTC](https://www.ruby-forum.com/t/p-is-to-inspect-as-pp-is-to/121708 "2007-11-30T04:32:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [November 30, 2007, 4:32am UTC](https://www.ruby-forum.com/t/p-is-to-inspect-as-pp-is-to/121708/1 "2007-11-30T04:32:20Z")

</div>

I have this as a lone method, but it seems to me there should already  
be a way to do this with the ‘pp’ lib. (If not perhaps we could add  
it?)

require ‘pp’  
require ‘stringio’

module Kernel

# Returns a pretty-printed string of the object. Requires libraries

+pp+ and

# +stringio+ from the Ruby standard library.

# 

# The following code pretty-prints an object (much like +p+ plain-

prints an

# object):

# 

# pp object

# 

# The following code captures the pretty-printing in +str+ instead

of

# sending it to +STDOUT+.

# 

# str = object.pp\_s

# 

# CREDIT Noah Gibbs

# CREDIT Gavin S.

def pp\_s  
pps = StringIO.new  
PP.pp(self, pps)  
pps.string  
end

end

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [December 1, 2007, 12:20am UTC](https://www.ruby-forum.com/t/p-is-to-inspect-as-pp-is-to/121708/2 "2007-12-01T00:20:20Z")

</div>

> I have this as a lone method, but it seems to me there should already  
> be a way to do this with the ‘pp’ lib. (If not perhaps we could add  
> it?)

I believe that pp calls Object#pretty\_print - the source will tell you  
more.

Dan.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [December 1, 2007, 1:04am UTC](https://www.ruby-forum.com/t/p-is-to-inspect-as-pp-is-to/121708/3 "2007-12-01T01:04:48Z")

</div>

On Nov 30, 2007 3:17 PM, Daniel S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > I have this as a lone method, but it seems to me there should already  
> > be a way to do this with the ‘pp’ lib. (If not perhaps we could add  
> > it?)
> 
> I believe that pp calls Object#pretty\_print - the source will tell you  
> more.

At least since 1.8.6 that method exists.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [December 1, 2007, 1:20am UTC](https://www.ruby-forum.com/t/p-is-to-inspect-as-pp-is-to/121708/4 "2007-12-01T01:20:06Z")

</div>

On Nov 30, 1:17 am, “Daniel S.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > I have this as a lone method, but it seems to me there should already  
> > be a way to do this with the ‘pp’ lib. (If not perhaps we could add  
> > it?)
> 
> I believe that pp calls Object#pretty\_print - the source will tell you  
> more.

I looked at that and didn’t see how that would help. As it says…

“To define your customized pretty printing function for your classes,  
redefine a method pretty\_print(pp) in the class. It takes an argument  
pp which is an instance of the class PP. The method should use  
PP#text, PP#breakable, PP#nest, PP#group and PP#pp to print the  
object.”

Don’t see off-hand how that can help easily get a string of what it  
would output.

Thanks,  
T.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [December 1, 2007, 2:01am UTC](https://www.ruby-forum.com/t/p-is-to-inspect-as-pp-is-to/121708/5 "2007-12-01T02:01:37Z")

</div>

On Nov 30, 2007 4:17 PM, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I looked at that and didn’t see how that would help. As it says…
> 
> “To define your customized pretty printing function for your classes,  
> redefine a method pretty\_print(pp) in the class. It takes an argument  
> pp which is an instance of the class PP. The method should use  
> PP#text, PP#breakable, PP#nest, PP#group and PP#pp to print the  
> object.”
> 
> Don’t see off-hand how that can help easily get a string of what it  
> would output.

Sorry, i had the wrong method in mind, what you want is most likely  
Object#pretty\_inspect  
Example:  
puts (1…30).to\_a.pretty\_inspect

Does that help?

^ manveru

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [December 1, 2007, 6:40am UTC](https://www.ruby-forum.com/t/p-is-to-inspect-as-pp-is-to/121708/6 "2007-12-01T06:40:59Z")

</div>

On Nov 30, 2:58 am, “Michael F.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Don’t see off-hand how that can help easily get a string of what it  
> > would output.
> 
> Sorry, i had the wrong method in mind, what you want is most likely  
> Object#pretty\_inspect  
> Example:  
> puts (1…30).to\_a.pretty\_inspect
> 
> Does that help?

w00t! w00t!

thanks manveru!

T.
