Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
A

awesome_print

> 编程语言
Open source

Pretty print your Ruby objects with style -- in full color and with proper indentation

4.1K stars0 likes1 views
WebsiteGitHub

About

Pretty print your Ruby objects with style -- in full color and with proper indentation

Awesome Print

Awesome Print is a Ruby library that pretty prints Ruby objects in full color exposing their internal structure with proper indentation. Rails ActiveRecord objects and usage within Rails templates are supported via included mixins.

NOTE: awesome_print v1.9.0 may not work on Ruby versions 2.4 or older or Rails versions 4.2 or older. The upcoming awesome_print v2.0 will require Ruby v1.9.3 or later and Rails v3.0 or later.

Installation

# Installing as Ruby gem
$ gem install awesome_print

# Cloning the repository
$ git clone git://github.com/awesome-print/awesome_print.git

Usage

require "awesome_print"
ap object, options = {}

Default options:

…

Supported color names:

:gray, :red, :green, :yellow, :blue, :purple, :cyan, :white
:black, :redish, :greenish, :yellowish, :blueish, :purpleish, :cyanish, :pale

Use Object#ai to return an ASCII encoded string:

irb> "awesome print".ai
=> "\e[0;33m\"awesome print\"\e[0m"

Examples

Array

$ cat > 1.rb
require "awesome_print"
data = [ false, 42, %w(forty two), { :now => Time.now, :class => Time.now.class, :distance => 42e42 } ]
ap data
^D
$ ruby 1.rb
[
    [0] false,
    [1] 42,
    [2] [
        [0] "forty",
        [1] "two"
    ],
    [3] {
           :class => Time < Object,
             :now => Fri Apr 02 19:55:53 -0700 2010,
        :distance => 4.2e+43
    }
]

Hash

$ cat > 2.rb
require "awesome_print"
data = { :now => Time.now, :class => Time.now.class, :distance => 42e42 }
ap data, :indent => -2  # <-- Left align hash keys.
^D
$ ruby 2.rb
{
  :class    => Time < Object,
  :now      => Fri Apr 02 19:55:53 -0700 2010,
  :distance => 4.2e+43
}

Nested array

$ cat > 3.rb
require "awesome_print"
data = [ false, 42, %w(forty two) ]
data << data  # <-- Nested array.
ap data, :multiline => false
^D
$ ruby 3.rb
[ false, 42, [ "forty", "two" ], [...] ]

Class methods

$ cat > 4.rb
require "awesome_print"
class Hello
  def self.world(x, y, z = nil, &blk)
  end
end
ap Hello.methods - Class.methods
^D
$ ruby 4.rb
[
    [0] world(x, y, *z, &blk) Hello
]

Object methods

…

Compare output to value

$ cat > 6.rb
require "awesome_print"
ap 42 == ap(42)
^D
$ ruby 6.rb
42
true

Array with default output limit

$ cat > 7.rb
require "awesome_print"
some_array = (1..1000).to_a
ap some_array, :limit => true
^D
$ ruby 7.rb
[
    [  0] 1,
    [  1] 2,
    [  2] 3,
    [  3] .. [996],
    [997] 998,
    [998] 999,
    [999] 1000
]

Array with specific output limit

$ cat > 8.rb
require "awesome_print"
some_array = (1..1000).to_a
ap some_array, :limit => 5
^D
$ ruby 8.rb
[
    [  0] 1,
    [  1] 2,
    [  2] .. [997],
    [998] 999,
    [999] 1000
]

Rails console

…

IRB integration

To use awesome_print as default formatter in irb and Rails console add the following code to your ~/.irbrc file:

require "awesome_print"
AwesomePrint.irb!

PRY integration

If you miss awesome_print's way of formatting output, here's how you can use it in place of the formatting which comes with pry. Add the following code to your ~/.pryrc or your project's ./.pryrc:

require "awesome_print"
AwesomePrint.pry!

Logger Convenience Method

awesome_print adds the 'ap' method to the Logger and ActiveSupport::BufferedLogger classes letting you call:

logger.ap object

By default, this logs at the :debug level. You can override that globally with:

:log_level => :info

in the custom defaults (see below). You can also override on a per call basis with:

logger.ap object, :warn

ActionView Convenience Method

awesome_print adds the 'ap' method to the ActionView::Base class making it available within Rails templates. For example:

<%= ap @accounts.first %>   # ERB
!= ap @accounts.first       # HAML

With other web frameworks (ex: in Sinatra templates) you can explicitly request HTML formatting:

<%= ap @accounts.first, :html => true %>

String Convenience Methods

Use methods such as .red to set string color:

irb> puts "red text".red
red text # (it's red)

Setting Custom Defaults

You can set your own default options by creating .aprc file in your home directory. Within that file assign your defaults to AwesomePrint.defaults. For example:

# ~/.aprc file.
AwesomePrint.defaults = {
  :indent => -2,
  :color => {
    :hash  => :pale,
    :class => :white
  }
}

Versioning

AwesomePrint follows the Semantic Versioning standard.

Contributing

See CONTRIBUTING.md for information.

License

Copyright (c) 2010-2016 Michael Dvorkin and contributors

http://www.dvorkin.net

%w(mike dvorkin.net) * "@" || "twitter.com/mid"

Released under the MIT license. See LICENSE file for details.

GitHub Issues· 0 open

View all on GitHub

No open issues yet, or sync has not completed.

Highlights

  • •Ruby

> Tags

Ruby

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言