Stefan Mahlitz
5/19/2007 3:59:00 PM
James Edward Gray II wrote:
> 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) %>
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