How to pp-enable a class?

H

Hal Fulton

Are there good docs or even a tutorial on how to enable a class
to be prettyprinted with pp?

It looks like the sort of thing I could figure out in 5 or 6 hours
of experimentation. I'd rather not spend those if I can help it.

I did read this link, which is a reference:
http://cvs.m17n.org/~akr/pp/pp.html

It contains among other things the tantalizing entry:

"To define your customized pretty printing function for your
class, redefine a method pretty_print(pp) in the class. It
takes an argument pp which is an instance of the class PP.
The method should use PP#text, PP#breakable, PP#nest, PP#group
and PP#pp to print the object."

But I don't grasp the details yet.


Hal
 
G

Gavin Sinclair

Are there good docs or even a tutorial on how to enable a class
to be prettyprinted with pp?
It looks like the sort of thing I could figure out in 5 or 6 hours
of experimentation. I'd rather not spend those if I can help it.

I've never expended any effort "preparing" a class for pp'ing. I just
pp it and am happy with the result.

Objects are just a recursive collection of objects and core types.
Core types have their pp defined, and objects are just structure. So
it should all Just Work.

Cheers,
Gavin
 
H

Hal Fulton

Gavin said:
I've never expended any effort "preparing" a class for pp'ing. I just
pp it and am happy with the result.

Objects are just a recursive collection of objects and core types.
Core types have their pp defined, and objects are just structure. So
it should all Just Work.

True in general.

But I have a case (hence my question) where it doesn't "just work"
and I'm not sure why.

I can't reproduce it simply yet, or I'd show you.


Hal
 
A

Ara.T.Howard

True in general.

But I have a case (hence my question) where it doesn't "just work"
and I'm not sure why.

I can't reproduce it simply yet, or I'd show you.


Hal

the other case where it does not work is in cases where you might want to use
#inspect, but do not because your object is not that pretty. here is an
example and my work around:

~ > cat a.rb
require 'pp'
require 'logger'

class Object
def pretty port = ''
PP::pp(self, port).strip
end
end

class Config < Hash; end


logger = Logger.new STDOUT
conf = Config[ 'key' => 'value' ]

logger.debug{ "conf <#{ conf.pretty }>" }


~ > ruby a.rb
D, [2004-05-24T17:42:13.811603 #23616] DEBUG -- : conf <{"key"=>"value"}>

IMHO if you require 'pp' Object#pretty should be defined automatically.

cheers.

-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
| URL :: http://www.ngdc.noaa.gov/stp/
| "640K ought to be enough for anybody." - Bill Gates, 1981
===============================================================================
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top