Save pp output to a string

hi, folks -

how do I save the output from pp to a string for later use?

what I had hoped for is something like this:

 x= [2, 3,4]
 res = eval "pp x"

but that doesn’t work.

thanks for help

Oliver

On Nov 20, 11:48 am, Oliver [email protected] wrote:

hi, folks -

how do I save the output from pp to a string for later use?

irb(main):001:0> require ‘pp’
=> true

irb(main):002:0> h = {(‘a’*60)=>[‘b’*60,‘c’*60],
(‘d’*60)=>[‘e’*60,‘f’*60]}
=>
{“dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd”=>[“eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee”,
“ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff”],
“aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa”=>[“bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb”,
“cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc”]}

irb(main):003:0> pp h
{“dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd”=>
[“eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee”,
“ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff”],
“aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa”=>
[“bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb”,
“cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc”]}
=> nil

irb(main):004:0> str = h.pretty_inspect
=> “{"dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"=>
\n ["eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
eeeeee",\n
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"],\n
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaa"=>\n
["bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",\n
"cccccccccccccccccccccccccccccccccccccc
cccccccccccccccccccccc"]}\n”

irb(main):005:0> puts str
{“dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd”=>
[“eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee”,
“ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff”],
“aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa”=>
[“bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb”,
“cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc”]}
=> nil

On Wed, Nov 21, 2007 at 03:50:06AM +0900, Oliver wrote:

hi, folks -

how do I save the output from pp to a string for later use?
require ‘pp’
x=[2,3,4]
string=""
PP.pp(x,string)
puts string
=> [2, 3, 4]

works for IO objects as well, or anything that supports <<.

exactly what I need, thanks very much.

Oliver

Hallelujah! I thought this was impossible.

On Nov 20, 2007 11:50 AM, Oliver [email protected] wrote:

how do I save the output from pp to a string for later use?
“cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc”]}
irb(main):004:0> str = h.pretty_inspect
irb(main):005:0> puts str
{“dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd”=>
[“eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee”,
“ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff”],
“aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa”=>
[“bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb”,
“cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc”]}
=> nil


Giles B.

Podcast: http://hollywoodgrit.blogspot.com
Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com