Tanaka Akira's PrettyPrint usage? Are there any examples?

E

Ed Baker

I'm interested in using the prettyprint module available from RAA.

But, as a relatively new Rubyist, I can't seem to make sense of the included RDOCS.

Has anyone got some simple examples they could share?
 
E

Ed Baker

Without sounding incredibly stupid, I want to know how to feed it ruby code and
get back formatted output.

I sort of expected a unix like sytax. i.e. a command line like:
ruby pp.rb myfile.rb > mynewfile.rb
 
E

Ed Baker

Also, did I make a mistake in downloading the pp application from RAA?
Since it's supposed to be a builtin to v1.8. Should I have left well enough
alone?

It's like what carburetor means in French: "If it works, don't touch it"
 
J

Jason Creighton

On 4 Sep 2003 04:10:32 -0700
I'm interested in using the prettyprint module available from RAA.

But, as a relatively new Rubyist, I can't seem to make sense of the
included RDOCS.

Has anyone got some simple examples they could share?

prettyprint is now included in Ruby, at least as of 1.8. Just from
reading the commands in prettyprint.rb, it appears prettyprint is a
module for building, well, pretty printers.

I can't share any examples for prettyprint, but I can share some for
'pp', a library which builds upon prettyprint to provide nice output for
Ruby objects. For example:
require 'pp' => true
ary = Array.new(5) { |i| Array.new(5) { |j| i*j } } => [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8], [0, 3, 6, 9, 12], [0, 4, 8, 12, 16]]
pp ary
[[0, 0, 0, 0, 0],
[0, 1, 2, 3, 4],
[0, 2, 4, 6, 8],
[0, 3, 6, 9, 12],
[0, 4, 8, 12, 16]]
=> nil[["PWD", "/usr/lib/ruby/1.8"],
["PS1", "\\w\\$ "],
["PS2", "> "],
["XWM", "ion"]]
=> nil

....and so on. YAML can help pretty-print as well:
h = {}; 5.times { |i| h = i**4 }; puts h.to_yaml

---
0: 0
1: 1
2: 16
3: 81
4: 256
=> nil

YAML defines a 'y' method that's shorthand for puts obj.to_yaml, ie:
---
goober: lala
3.1415: PI
123: 456
=> nil

Fun stuff.

Jason Creighton
 
W

why the lucky stiff

Ed said:
I sort of expected a unix like sytax. i.e. a command line like:
ruby pp.rb myfile.rb > mynewfile.rb

If you run pp.rb from the commandline, the library will perform some
basic tests to ensure it is working. Oh, wait a sec. I see what you're
thinking. You're looking for something like lint, for beautifying Ruby
code.

PP is for displaying data structures in Ruby, so it won't do the trick.
Not sure what will.

_why
 

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,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top