Is there a way to make the output of inspect more readable?
In Perl, there is Data::Dumper
which makes the output more easy to read.
The ruby standard library contains PP (short for "Pretty Print"), which formats structures much better than the standard inspect:
http://www.ruby-doc.org/stdlib/libdoc/pp/rdoc/index.html
You need to require 'pp'
at the top of your source file before you can use it, and then just replace p obj
with pp obj
in your code.
I find it really useful for hashes and arrays in particular!
like @tjbp said, just simply use pretty_inspect()
instead of inspect()
it will give you nice line break and indentation
Beside the 'pp' solution, yaml may be a solution.
Try:
require 'yaml'
puts [1,2,3=>'three'].to_yaml
You get:
---
- 1
- 2
- 3: three
And a complete different approach: Write your own inspect-code if you have a inspect-problem with a specific class
Example:
class MyTest
def initialize()
@created = Time.now
end
def inspect()
"Hi, I'm the objected created at #{@created}. That's #{Time.now - @created} seconds ago"
end
end
x = MyTest.new
sleep 5
puts x.inspect
You get
Hi, I'm the objected created at 2011-06-28 12:48:38 +0100. That's 5.0 seconds ago
This will work and look great :)
in your gemfile:
group :development do
gem 'table_print', '~> 1.5.3'
end
in your view:
<pre>
<%= TablePrint::Printer.new(@users, []).table_print %>
</pre>