Want to stay on top? Ruby Weekly is a once-weekly e-mail newsletter covering the latest Ruby and Rails news.
     Feed Icon

awesome_print: A New Pretty Printer for your Ruby Objects

By Peter Cooper / April 7, 2010

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.

Comments

  1. Trevor Menagh says:

    What a great tool! Thanks for showcasing it.

  2. kenny says:

    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.

  3. Justin @ PickFu says:

    Very nice. Definitely makes it easier to read objects with lots of attributes.

  4. Jim says:

    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

  5. Michael Dvorkin says:

    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! :-)

  6. hooopo says:

    awesome!! thanks a lot!

  7. Olivier BONNAURE says:

    Nice tool, but it require 'active_record' to works properly !

  8. Ryan Sandridge says:

    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

  9. Trans says:

    Well, the color's nice. But I always just used #y when I needed nicer output.

  10. manveru says:

    A quick comparision of y and ap:

    ap "オリジナル" # => "オリジナル"
    y "オリジナル" # => --- "\xE3\x82\xAA\xE3\x83\xAA\xE3\x82\xB8\xE3\x83\x8A\xE3\x83\xAB"

  11. Suraj N. Kurapati says:

    @Trans, @manveru What's this mysterious #y method? It's hard to google it... could you please provide a link?

  12. Evgeniy Dolzhenko says:

    @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)`

  13. Michael Dvorkin says:

    awesome_print 0.2.0 is now available: http://www.rubyflow.com/items/3857

Other Posts to Enjoy

Twitter Mentions