# PrettyPrint to a web page?

**URL:** <https://www.ruby-forum.com/t/prettyprint-to-a-web-page/104534>\
**Category:** Ruby\
**Created:** [May 19, 2007, 1:40am UTC](https://www.ruby-forum.com/t/prettyprint-to-a-web-page/104534 "2007-05-19T01:40:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mario\_TSL](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df705f/32.png) [@Mario\_TSL](https://www.ruby-forum.com/u/Mario_TSL)\
**Post date:** [May 19, 2007, 1:40am UTC](https://www.ruby-forum.com/t/prettyprint-to-a-web-page/104534/1 "2007-05-19T01:40:58Z")

</div>

This is gonna sound like a dumb question to you Ruby veterans, but: How  
do you dump pretty\_print contents to a web page from within a Rails app?

I’ve tried this and a few other things from a view:

# Session Info
\<% require 'pp' %\> \<%= pp(session) %\>

---

<div class="post-metadata">

**Author:** ![Mario\_TSL](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df705f/32.png) [@Mario\_TSL](https://www.ruby-forum.com/u/Mario_TSL)\
**Post date:** [May 19, 2007, 4:32pm UTC](https://www.ruby-forum.com/t/prettyprint-to-a-web-page/104534/2 "2007-05-19T16:32:47Z")

</div>

Mario T. Lanza wrote:

> This is gonna sound like a dumb question to you Ruby veterans, but: How  
> do you dump pretty\_print contents to a web page from within a Rails app?
> 
> I’ve tried this and a few other things from a view:
> 
> # Session Info
> \<% require 'pp' %\> \<%= pp(session) %\>

I don’t know with PP, but I’m using this sometimes

\<%=h session.inspect %\>

Stefan

---

<div class="post-metadata">

**Author:** ![Mario\_TSL](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df705f/32.png) [@Mario\_TSL](https://www.ruby-forum.com/u/Mario_TSL)\
**Post date:** [May 19, 2007, 5:27pm UTC](https://www.ruby-forum.com/t/prettyprint-to-a-web-page/104534/3 "2007-05-19T17:27:52Z")

</div>

On May 18, 2007, at 6:40 PM, Mario T. Lanza wrote:

> This is gonna sound like a dumb question to you Ruby veterans, but:  
> How  
> do you dump pretty\_print contents to a web page from within a Rails  
> app?
> 
> I’ve tried this and a few other things from a view:
> 
> # Session Info
> \<% require 'pp' %\> \<%= pp(session) %\>

Change that last line to:

\<%= PP.pp(session, String.new) %\>

pp() accepts a second argument to specify where to put the output.  
This just defaults to $\> ($stdout). If we specify a String, the  
output is collected there instead:

> > require “pp”  
> > =\> false
> 
> > Contact = Struct.new(:name, :city, :state, :email)  
> > =\> Contact
> 
> > james = Contact.new(“James Edward G. II”, “Edmond”, “Oklahoma”,  
> > “removed\_email\_address@domain.invalid”)  
> > =\> #\<struct Contact name=“James Edward G. II”, city=“Edmond”,  
> > state=“Oklahoma”, email=“removed\_email\_address@domain.invalid”\>
> 
> > pp\_james = String.new  
> > =\> “”
> 
> > PP.pp(james, pp\_james)  
> > =\> “#\<struct Contact\n name=“James Edward G. II”,\n city=“Edmond”,  
> > \n state=“Oklahoma”,\n email=“removed\_email\_address@domain.invalid”\>\n”
> 
> > puts pp\_james  
> > #\<struct Contact  
> > name=“James Edward G. II”,  
> > city=“Edmond”,  
> > state=“Oklahoma”,  
> > email=“removed\_email\_address@domain.invalid”\>  
> > =\> nil

James Edward G. II

---

<div class="post-metadata">

**Author:** ![Mario\_TSL](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df705f/32.png) [@Mario\_TSL](https://www.ruby-forum.com/u/Mario_TSL)\
**Post date:** [May 19, 2007, 6:41pm UTC](https://www.ruby-forum.com/t/prettyprint-to-a-web-page/104534/4 "2007-05-19T18:41:46Z")

</div>

Both of these work, thanks.

```
<%=h session.inspect %>
```

```
<%= PP.pp(session, String.new) %>
```

They both have their advantages. The first includes additional  
information I want and the second is easier to read.

How would we merging the two results?

---

<div class="post-metadata">

**Author:** ![Mario\_TSL](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df705f/32.png) [@Mario\_TSL](https://www.ruby-forum.com/u/Mario_TSL)\
**Post date:** [May 19, 2007, 6:43pm UTC](https://www.ruby-forum.com/t/prettyprint-to-a-web-page/104534/5 "2007-05-19T18:43:52Z")

</div>

Duh! Nevermind that last question.

This produced the best results:

```
<%=h PP.pp(session, String.new) %>
```

---

<div class="post-metadata">

**Author:** ![Mario\_TSL](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df705f/32.png) [@Mario\_TSL](https://www.ruby-forum.com/u/Mario_TSL)\
**Post date:** [May 19, 2007, 5:59pm UTC](https://www.ruby-forum.com/t/prettyprint-to-a-web-page/104534/6 "2007-05-19T17:59:54Z")

</div>

James Edward G. II wrote:

> Change that last line to:
> 
> \<%= PP.pp(session, String.new) %\>

Using \<%=h something %\> is better in this case, as #SomeClass:0x...  
will be escaped (the angle brackets) during template processing.

> pp() accepts a second argument to specify where to put the output. This  
> just defaults to $\> ($stdout). If we specify a String, the output is  
> collected there instead:

Only when specifying PP as receiver for ‘pp’.

irb(main):001:0\> require “pp”  
=\> true  
irb(main):002:0\> output = “”  
=\> “”  
irb(main):003:0\> data = self.class.constants.select {|x| x.length \< 5}  
=\> [“IRB”, “ARGF”, “ENV”, “SLex”, “IO”, “Proc”, “GC”, “Hash”, “TRUE”,  
“File”, “NIL”, “PP”, “Time”, “Data”, “Dir”, “ARGV”, “Math”]  
irb(main):004:0\> pp(data, output)  
[“IRB”,  
“ARGF”,  
“ENV”,  
“SLex”,  
“IO”,  
“Proc”,  
“GC”,  
“Hash”,  
“TRUE”,  
“File”,  
“NIL”,  
“PP”,  
“Time”,  
“Data”,  
“Dir”,  
“ARGV”,  
“Math”]  
“”  
=\> nil  
irb(main):005:0\> output  
=\> “”

Well, good to see a working version, thanks James Edward.

Stefan
