Forum: Ruby PrettyPrint to a web page?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D9e66bb7361fea87a658ceecb2f547d6?d=identicon&s=25 Mario T. Lanza (mlanza)
on 2007-05-19 01:40
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:

<h1>Session Info</h1>
<% require 'pp' %>
<%= pp(session) %>
428f96cc689eb7419bba3a8bbfcc222a?d=identicon&s=25 Stefan Mahlitz (Guest)
on 2007-05-19 16:32
(Received via mailing list)
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:
>
> <h1>Session Info</h1>
> <% require 'pp' %>
> <%= pp(session) %>

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

<%=h session.inspect %>

Stefan
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2007-05-19 17:27
(Received via mailing list)
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:
>
> <h1>Session Info</h1>
> <% 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 Gray II", "Edmond", "Oklahoma",
"james@grayproductions.net")
=> #<struct Contact name="James Edward Gray II", city="Edmond",
state="Oklahoma", email="james@grayproductions.net">
 >> pp_james = String.new
=> ""
 >> PP.pp(james, pp_james)
=> "#<struct Contact\n name="James Edward Gray II",\n city="Edmond",
\n state="Oklahoma",\n email="james@grayproductions.net">\n"
 >> puts pp_james
#<struct Contact
name="James Edward Gray II",
city="Edmond",
state="Oklahoma",
email="james@grayproductions.net">
=> nil

James Edward Gray II
428f96cc689eb7419bba3a8bbfcc222a?d=identicon&s=25 Stefan Mahlitz (Guest)
on 2007-05-19 17:59
(Received via mailing list)
James Edward Gray 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
D9e66bb7361fea87a658ceecb2f547d6?d=identicon&s=25 Mario T. Lanza (mlanza)
on 2007-05-19 18:41
Both of these work, thanks.

<pre>
<%=h session.inspect %>
</pre>

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

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?
D9e66bb7361fea87a658ceecb2f547d6?d=identicon&s=25 Mario T. Lanza (mlanza)
on 2007-05-19 18:43
Duh! Nevermind that last question.

This produced the best results:

<pre>
<%=h PP.pp(session, String.new) %>
</pre>
This topic is locked and can not be replied to.