pretty_print woes

R

Ronald Fischer

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=3Dfoo(bar)
$my_message=3D"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=20
to a string?

Ronald
--=20
Ronald Fischer <[email protected]>
Phone: +49-89-452133-162
=20
 
R

Robert Klemme

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

irb(main):009:0> puts "foo\"bar".pretty_inspect
"foo\"bar"
=> nil

robert
 
R

Ronald Fischer

require 'pp'
irb(main):009:0> puts "foo\"bar".pretty_inspect
"foo\"bar"
=3D> nil

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

Ronald
 
J

John Joyce

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!
 
R

Robert Klemme

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 = 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
 

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,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top