# Pretty\_print woes

**URL:** <https://www.ruby-forum.com/t/pretty-print-woes/108014>\
**Category:** Ruby\
**Created:** [June 28, 2007, 1:32pm UTC](https://www.ruby-forum.com/t/pretty-print-woes/108014 "2007-06-28T13:32:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [June 28, 2007, 1:32pm UTC](https://www.ruby-forum.com/t/pretty-print-woes/108014/1 "2007-06-28T13:32:12Z")

</div>

I like the formatting one gets by

require ‘pp’  
pp foo() # etc.

In my application however, I don’t want the pp-formatted string to be  
printed, but to be returned as a string to my application. Searching  
the Net, I came up with the following solution (Example):

require ‘pp’  
…  
result=foo(bar)  
$my\_message=“The function call foo(”+  
bar.pretty\_print\_inspect+  
") returned: "+result.pretty\_print\_inspect

This works well most of the time, for example if the values formatted  
with pretty\_print\_inspect are arrays or numbers. I get, however, a  
runtime error when calling for String:

“abc”.pretty\_print\_inspect  
RuntimeError: pretty\_print is not overridden for String

Is there a different way to use the module pp, so that its output goes  
to a string?

Ronald

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [June 28, 2007, 2:02pm UTC](https://www.ruby-forum.com/t/pretty-print-woes/108014/2 "2007-06-28T14:02:16Z")

</div>

On 28.06.2007 13:31, Ronald F. wrote:

> …  
> RuntimeError: pretty\_print is not overridden for String
> 
> Is there a different way to use the module pp, so that its output goes  
> to a string?
> 
> Ronald

irb(main):009:0\> puts “foo"bar”.pretty\_inspect  
“foo"bar”  
=\> nil

robert

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [June 28, 2007, 3:28pm UTC](https://www.ruby-forum.com/t/pretty-print-woes/108014/3 "2007-06-28T15:28:31Z")

</div>

> > runtime error when calling for String:
> > 
> > “abc”.pretty\_print\_inspect  
> > RuntimeError: pretty\_print is not overridden for String
> > 
> > Is there a different way to use the module pp, so that its  
> > output goes  
> > to a string?  
> > irb(main):009:0\> puts “foo"bar”.pretty\_inspect  
> > “foo"bar”  
> > =\> nil

Thanks a lot! Nearly perfect!! (Actually, I use pretty\_inspect.chomp,  
because pretty\_inspect adds a \n to the formatted string).

Ronald

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [June 28, 2007, 3:43pm UTC](https://www.ruby-forum.com/t/pretty-print-woes/108014/4 "2007-06-28T15:43:34Z")

</div>

On Jun 28, 2007, at 8:26 AM, Ronald F. wrote:

> > > runtime error when calling for String:
> 
> Thanks a lot! Nearly perfect!! (Actually, I use pretty\_inspect.chomp,  
> because pretty\_inspect adds a \n to the formatted string).
> 
> Ronald

Actually, puts is adding that \n to the string, but your result is  
what you wanted!

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [June 28, 2007, 11:26pm UTC](https://www.ruby-forum.com/t/pretty-print-woes/108014/5 "2007-06-28T23:26:54Z")

</div>

On 28.06.2007 15:42, John J. wrote:

> > > > This works well most of the time, for example if the values  
> > > > irb(main):009:0\> puts “foo"bar”.pretty\_inspect  
> > > > “foo"bar”  
> > > > =\> nil
> > 
> > Thanks a lot! Nearly perfect!! (Actually, I use pretty\_inspect.chomp,  
> > because pretty\_inspect adds a \n to the formatted string).
> > 
> > Ronald
> 
> Actually, puts is adding that \n to the string, but your result is what  
> you wanted!

No, #pretty\_inspect actually adds it:

irb(main):001:0\> “a”.pretty\_inspect  
=\> ““a”\n”

Ronald, #pretty\_inspect can actually add newlines itself:

irb(main):004:0\> ha=(1…10).inject({}) {|h,i| h[i] = Array.new(i, i); h}  
=\> {5=\>[5, 5, 5, 5, 5], 6=\>[6, 6, 6, 6, 6, 6], 1=\>[1], 7=\>[7, 7, 7, 7,  
7, 7, 7], 2=\>[2, 2], 8=\>[8, 8, 8, 8, 8, 8, 8, 8],  
3=\>[3, 3, 3], 9=\>[9, 9, 9, 9, 9, 9, 9, 9, 9], 4=\>[4, 4, 4, 4],  
10=\>[10, 10, 10, 10, 10, 10, 10, 10, 10, 10]}  
irb(main):005:0\> ha.pretty\_inspect  
=\> “{5=\>[5, 5, 5, 5, 5],\n 6=\>[6, 6, 6, 6, 6, 6],\n 1=\>[1],\n 7=\>[7, 7,  
7, 7, 7, 7, 7],\n 2=\>[2, 2],\n 8=\>[8, 8, 8, 8, 8  
, 8, 8, 8],\n 3=\>[3, 3, 3],\n 9=\>[9, 9, 9, 9, 9, 9, 9, 9, 9],\n 4=\>[4,  
4, 4, 4],\n 10=\>[10, 10, 10, 10, 10, 10, 10, 10,  
10, 10]}\n”  
irb(main):006:0\> puts ha.pretty\_inspect  
{5=\>[5, 5, 5, 5, 5],  
6=\>[6, 6, 6, 6, 6, 6],  
1=\>[1],  
7=\>[7, 7, 7, 7, 7, 7, 7],  
2=\>[2, 2],  
8=\>[8, 8, 8, 8, 8, 8, 8, 8],  
3=\>[3, 3, 3],  
9=\>[9, 9, 9, 9, 9, 9, 9, 9, 9],  
4=\>[4, 4, 4, 4],  
10=\>[10, 10, 10, 10, 10, 10, 10, 10, 10, 10]}  
=\> nil  
irb(main):007:0\>

Kind regards

robert
