pp to a string, not stdout

L

Larry Kluger

Hi,

pp foo # pretty prints foo to stdout

But I want to print to a string, which I'll then be handling in rails.

I tried the following and output still went to the Mongrel log (stdout)

Any help would be most appreciated.

Thanks,

Larry

~~~~~~~~~
s = StringIO.new
pp(fields, s)
s.rewind
s.read # Should be the output of pretty print, but = '' instead
 
J

Joel VanderWerf

Larry said:
Hi,

pp foo # pretty prints foo to stdout

But I want to print to a string, which I'll then be handling in rails.

irb(main):009:0> [1,2,3].pretty_print_inspect
=> "[1, 2, 3]"
irb(main):010:0> [1,2,3].pretty_inspect
=> "[1, 2, 3]\n"
 
M

Marcel Molina Jr.

pp foo # pretty prints foo to stdout

But I want to print to a string, which I'll then be handling in rails.

irb(main):009:0> [1,2,3].pretty_print_inspect
=> "[1, 2, 3]"
irb(main):010:0> [1,2,3].pretty_inspect
=> "[1, 2, 3]\n"

There is also the quite awkward:

irb(main):003:0> PP.pp((1..30).to_a, s = '')
=> "[1,\n 2,\n 3,\n 4,\n 5,\n 6,\n 7,\n 8,\n 9,\n 10,\n 11,\n 12,\n 13,\n 14,\n 15,\n 16,\n 17,\n 18,\n 19,\n 20,\n 21,\n 22,\n 23,\n 24,\n 25,\n 26,\n 27,\n 28,\n 29,\n 30]\n"
irb(main):004:0> s
=> "[1,\n 2,\n 3,\n 4,\n 5,\n 6,\n 7,\n 8,\n 9,\n 10,\n 11,\n 12,\n 13,\n 14,\n 15,\n 16,\n 17,\n 18,\n 19,\n 20,\n 21,\n 22,\n 23,\n 24,\n 25,\n 26,\n 27,\n 28,\n 29,\n 30]\n"

marcel
 
L

Larry Kluger

Dear Joel and Marcel,

Thank you!

I appreciate your help.

Regards from a slightly steamy New York City,

Larry
 

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,792
Messages
2,569,639
Members
45,352
Latest member
SherriePet

Latest Threads

Top