以漂亮的风格打印 Ruby 对象 - 全彩显示,并使用适当的缩进
| 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.
# Installing as Ruby gem
$ gem install amazing_print
# Cloning the repository
$ git clone git://github.com/amazing-print/amazing_print.gitrequire "amazing_print"
ap object, options = {}Default options:
…Supported color names:
:gray, :red, :green, :yellow, :blue, :purple, :cyan, :white
:grayish, :redish, :greenish, :yellowish, :blueish, :purpleish, :cyanish, :whiteishUse Object#ai to return an ASCII encoded string:
irb> "awesome print".ai
=> "\e[0;33m\"awesome print\"\e[0m"……To use amazing_print as the default formatter in irb and the Rails console, add the following
code to your ~/.irbrc file:
require "amazing_print"
AmazingPrint.irb!
AmazingPrint.rdbg! # Enables integration with ruby debugIf 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:
require "amazing_print"
AmazingPrint.pry!amazing_print adds the ap method to the Logger and ActiveSupport::BufferedLogger classes
letting you call:
logger.ap objectBy default, this logs at the :debug level. You can override that globally with:
:log_level => :infoin 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: :warnYou 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 }amazing_print adds the ap method to the ActionView::Base class making it available
within Rails templates. For example:
# ERB
!= ap @accounts.first # HAMLWith other web frameworks (for example in Sinatra templates) you can explicitly request HTML formatting:
true %>Use methods such as .red to set string color:
irb> puts AmazingPrint::Colors.red("red text")
red text # (it's red)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:
# ~/.config/aprc file.
AmazingPrint.defaults = {
:indent => -2,
:color => {
:hash => :whiteish,
:class => :white
}
}The previous ~/.aprc location is still supported as a fallback.
AmazingPrint follows the Semantic Versioning standard.
See CONTRIBUTING.md for information.
Copyright (c) 2010-2016 Michael Dvorkin and contributors
%w(mike dvorkin.net) * "@" || "twitter.com/mid"
Released under the MIT license. See LICENSE file for details.
暂无开放 Issues,或尚未同步最近议题。