Pretty inspecting objects?

Hello!

Idea would be to write contents of some objects into some file in pretty
inspection format.

How is it possible in Ruby to get pretty inspection of an object without
printing it to stdout?

One possibility would be to play with stdout redirection while using
‘pp’, but that’s not what i’m looking for. Is there any easy way to do
it or should i start monkey-patching ‘pp’ to add that feature?

Jarmo

Jarmo P. wrote:

Idea would be to write contents of some objects into some file in pretty
inspection format.

How is it possible in Ruby to get pretty inspection of an object without
printing it to stdout?

One possibility would be to play with stdout redirection while using
‘pp’, but that’s not what i’m looking for. Is there any easy way to do
it or should i start monkey-patching ‘pp’ to add that feature?

Why monkey-patch? Look at source of pp.rb, and you’ll see it takes an
optional argument, out=$>

require ‘pp’
a = {:foo=>1,:bar=>2}
File.open("/tmp/pp.out",“w”) { |f| PP.pp(a, f) }

On Mon, Jun 28, 2010 at 2:02 PM, Jarmo P. [email protected]
wrote:

it or should i start monkey-patching ‘pp’ to add that feature?

You might want to have a look at awesome print - a gem that is a
functional
replacement for pretty print.

Richard C. wrote:

On Mon, Jun 28, 2010 at 2:02 PM, Jarmo P. [email protected]
wrote:

it or should i start monkey-patching ‘pp’ to add that feature?

You might want to have a look at awesome print - a gem that is a
functional
replacement for pretty print.

Thank you for the answers!

I also noticed that there is a #pretty_inspect method after requiring
‘pp’:
a.pretty_inspect does exactly the thing i want - returns a string.

I’ll look at that awesome print gem also :slight_smile:

Jarmo