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

T

Trans

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 Sinclair

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

end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,161
Latest member
GertrudeMa
Top