awesome_print: A New Pretty Printer for your Ruby Objects
awesome_print is a Ruby tool that provides "pretty printing" support for your objects. It's a bit like p
, pp
or, if you prefer, puts obj.inspect
, but with significantly improved, contextual, colored output. Its creator and maintainer is Michael Dvorkin of Fat Free CRM fame.
Being able to see "inside" Ruby objects on the fly can prove useful whether you're debugging some code your tests did not dare reach or you're just playing around in irb
. The most common way to examine objects is with p
or the inspect
method, but these don't format their output in a particularly easy-to-read way. pp
- part of the standard library - is a pretty printer that improves matters but it still leaves a lot to be desired. awesome_print takes it all to the next level.
A visual comparison between pp
and awesome_print proves compelling:
awesome_print's most compelling features are showing you the index of array elements within its output, inheritance for the classes of embedded objects, and color coding. Further, it's highly customizable, with the ability to set indent levels as well as the colors for each type of data shown.
To get up and running, gem install awesome_print
and then require 'ap'
and use ap
in place on anywhere you'd usually use p
. Yep, that's it.
April 7, 2010 at 12:36 pm
What a great tool! Thanks for showcasing it.
April 7, 2010 at 2:00 pm
This is awesome.
It would be nice to have a version paired down with sensible defaults put into a single include file, so we could just put it in our source tree.
April 7, 2010 at 4:45 pm
Very nice. Definitely makes it easier to read objects with lots of attributes.
April 8, 2010 at 4:31 am
Thanks for this :D
I forked the plugin and added an OPTIONS constant so configuring the plugin will be easy.
Just include the following lines in your .irbrc
require 'ap'
AwesomePrint::OPTIONS.merge!(
:multiline => true,
:indent => 2
)
for automatic loading of your configuration.
fork at http://bit.ly/a2LfqA
April 8, 2010 at 4:41 am
I've added support for storing custom defaults in ~/.aprc file. Check out awesome_print 0.1.3 http://github.com/michaeldv/awesome_print
Enjoy! :-)
April 8, 2010 at 5:50 am
awesome!! thanks a lot!
April 8, 2010 at 8:58 am
Nice tool, but it require 'active_record' to works properly !
April 9, 2010 at 3:51 pm
Just add this to your .irbrc if you want all return values to print via awesome_print:
require 'ap'
module IRB
class Irb
def output_value
ap @context.last_value
end
end
end
April 9, 2010 at 10:34 pm
Well, the color's nice. But I always just used #y when I needed nicer output.
April 10, 2010 at 4:08 am
A quick comparision of y and ap:
ap "オリジナル" # => "オリジナル"
y "オリジナル" # => --- "\xE3\x82\xAA\xE3\x83\xAA\xE3\x82\xB8\xE3\x83\x8A\xE3\x83\xAB"
April 18, 2010 at 7:44 pm
@Trans, @manveru What's this mysterious #y method? It's hard to google it... could you please provide a link?
April 26, 2010 at 1:05 pm
@Suraj http://github.com/shyouhei/ruby/blob/trunk/ext/syck/lib/syck.rb#L436
In other words `y` is the same as `puts(arg.to_yaml)`
May 6, 2010 at 5:36 am
awesome_print 0.2.0 is now available: http://www.rubyflow.com/items/3857