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.

Comments

  1. Trevor Menagh ·

    What a great tool! Thanks for showcasing it.

  2. kenny ·

    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 ·

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

  4. Jim ·

    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 ·

    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 ·

    awesome!! thanks a lot!

  7. Olivier BONNAURE ·

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

  8. Ryan Sandridge ·

    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 ·

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

  10. manveru ·

    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 ·

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

  12. Evgeniy Dolzhenko ·

    @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 ·

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