[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

pp to a string, not stdout

Larry Kluger

7/19/2007 2:36:00 AM


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

--
Posted via http://www.ruby-....

3 Answers

Joel VanderWerf

7/19/2007 2:55:00 AM

0

Larry Kluger wrote:
> 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"


--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Marcel Molina Jr.

7/19/2007 3:00:00 AM

0

On Thu, Jul 19, 2007 at 11:54:53AM +0900, Joel VanderWerf wrote:
>
> >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
--
Marcel Molina Jr. <marcel@vernix.org>

Larry Kluger

7/19/2007 3:15:00 AM

0

Dear Joel and Marcel,

Thank you!

I appreciate your help.

Regards from a slightly steamy New York City,

Larry

--
Posted via http://www.ruby-....