pretty inspecting objects?

J

Jarmo Pertman

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
 
B

Brian Candler

Jarmo said:
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) }
 
R

Richard Conroy

[Note: parts of this message were removed to make it a legal post.]

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?
You might want to have a look at awesome print - a gem that is a functional
replacement for pretty print.
 
J

Jarmo Pertman

Richard said:
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 :)

Jarmo
 

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,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top