PrettyPrint to a web page?

M

Mario T. Lanza

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) %>
 
S

Stefan Mahlitz

Mario said:
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
 
J

James Edward Gray II

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:
"(e-mail address removed)")
=> #<struct Contact name="James Edward Gray II", city="Edmond",
state="Oklahoma", email="(e-mail address removed)">=> "#<struct Contact\n name="James Edward Gray II",\n city="Edmond",
\n state="Oklahoma",\n email="(e-mail address removed)">\n"#<struct Contact
name="James Edward Gray II",
city="Edmond",
state="Oklahoma",
email="(e-mail address removed)">
=> nil

James Edward Gray II
 
S

Stefan Mahlitz

James said:
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:

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
 
M

Mario T. Lanza

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?
 
M

Mario T. Lanza

Duh! Nevermind that last question.

This produced the best results:

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

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
474,259
Messages
2,571,295
Members
47,931
Latest member
alibsskamoSeAve

Latest Threads

Top