# Pp to a string, not stdout

**URL:** <https://www.ruby-forum.com/t/pp-to-a-string-not-stdout/109799>\
**Category:** Ruby\
**Created:** [July 19, 2007, 4:35am UTC](https://www.ruby-forum.com/t/pp-to-a-string-not-stdout/109799 "2007-07-19T04:35:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [July 19, 2007, 4:35am UTC](https://www.ruby-forum.com/t/pp-to-a-string-not-stdout/109799/1 "2007-07-19T04:35:45Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [July 19, 2007, 4:56am UTC](https://www.ruby-forum.com/t/pp-to-a-string-not-stdout/109799/2 "2007-07-19T04:56:18Z")

</div>

Larry K. 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”

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [July 19, 2007, 5:00am UTC](https://www.ruby-forum.com/t/pp-to-a-string-not-stdout/109799/3 "2007-07-19T05:00:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [July 19, 2007, 5:14am UTC](https://www.ruby-forum.com/t/pp-to-a-string-not-stdout/109799/4 "2007-07-19T05:14:31Z")

</div>

Dear Joel and Marcel,

Thank you!

I appreciate your help.

Regards from a slightly steamy New York City,

Larry
