How to capture PrettyPrint output to string?

L

List Recv

I'd like to capture the output of pp (PrettyPrint) as a string, as
opposed to putting it on stdout.

Something like:
dump = myvar.pp

dump.class # => String

(Using .pretty_print_inspect is not adequate, as it's not the same as
pp, has no newlines, etc.)

Thanks!
 
R

Ryan Leavengood

require 'pp'
require 'stringio'

def my_pp(*args)
old_out =3D $stdout
begin
s=3DStringIO.new
$stdout=3Ds
pp(*args)
ensure
$stdout=3Dold_out
end
s.string
end
 
G

Gavin Sinclair

List said:
I'd like to capture the output of pp (PrettyPrint) as a string, as
opposed to putting it on stdout.

Something like:
dump = myvar.pp

http://extensions.rubyforge.org defines Object#pp_s which does what you
want. You can examine the implementation on the website.

The Facets/Core project may have something similar. Hopefully with a
nicer name!

This functionality is a screaming omission from the standard library!

Gavin
 
A

ara.t.howard

Hi,

At Thu, 8 Dec 2005 11:54:04 +0900,
List Recv wrote in [ruby-talk:169485]:
I'd like to capture the output of pp (PrettyPrint) as a string, as
opposed to putting it on stdout.

dump = PP.pp(myvar, "")

i usually use

PP.pp myvar, dump = ""

;-)

-a
--
===============================================================================
| ara [dot] t [dot] howard [at] noaa [dot] gov
| all happiness comes from the desire for others to be happy. all misery
| comes from the desire for oneself to be happy.
| -- bodhicaryavatara
===============================================================================
 

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,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top