百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
A

amazing_print

> 编程语言
开源

以漂亮的风格打印 Ruby 对象 - 全彩显示,并使用适当的缩进

853 stars0 点赞6 次浏览
访问官网GitHub

工具介绍

以漂亮的风格打印 Ruby 对象 - 全彩显示,并使用适当的缩进

Amazing Print

RubyGems RubyGems |

| AmazingPrint is a fork of AwesomePrint, which became stale, and should be used in its place to avoid conflicts. It 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.

Supported Versions

  • Ruby >= 3.2
  • Rails >= 6.1

Installation

# Installing as Ruby gem
$ gem install amazing_print

# Cloning the repository
$ git clone git://github.com/amazing-print/amazing_print.git

Usage

ruby
require "amazing_print"
ap object, options = {}

Default options:

…

Supported color names:

ruby
:gray, :red, :green, :yellow, :blue, :purple, :cyan, :white
:grayish, :redish, :greenish, :yellowish, :blueish, :purpleish, :cyanish, :whiteish

Use Object#ai to return an ASCII encoded string:

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

Examples

…

Example (Rails console)

…

IRB integration

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

ruby
require "amazing_print"
AmazingPrint.irb!
AmazingPrint.rdbg! # Enables integration with ruby debug

PRY integration

If you miss amazing_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:

ruby
require "amazing_print"
AmazingPrint.pry!

Logger Convenience Method

amazing_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 it on a per call basis with:

logger.ap object, :warn
# or
logger.ap object, level: :warn

You can also pass additional options (providing nil or leaving off level will log at the default level):

logger.ap object, { level: :info, sort_keys: true }

ActionView Convenience Method

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

   # ERB
!= ap @accounts.first       # HAML

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

 true %>

Colorizing Strings

Use methods such as .red to set string color:

ruby
irb> puts AmazingPrint::Colors.red("red text")
red text # (it's red)

Setting Custom Defaults

You can set your own default options by creating an aprc file in your $XDG_CONFIG_HOME directory (which defaults to ~/.config if undefined). Within that file assign your defaults to AmazingPrint.defaults. For example:

ruby
# ~/.config/aprc file.
AmazingPrint.defaults = {
  :indent => -2,
  :color => {
    :hash  => :whiteish,
    :class => :white
  }
}

The previous ~/.aprc location is still supported as a fallback.

Versioning

AmazingPrint 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.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rubyhacktoberfest

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

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