Ruby on Rails:variable.hash_set.inspect 的漂亮打印...有没有办法在控制台中漂亮地打印 .inpsect? [英] Ruby on Rails: pretty print for variable.hash_set.inspect ... is there a way to pretty print .inpsect in the console?
问题描述
我发现自己在我的功能测试中做了很多 puts .inpsect 来确保我知道数据是如何格式化的......但是当哈希对象中的每个条目之后没有新行时,哈希很难读取.无论如何,也许是一个宝石?,漂亮地打印哈希?
I findy myself doing a lot of puts .inpsect s in my functional testing to make sure I know how the data is formatted... but hashes are hard to read when there is no new lines after each entry in a hash object. Is there anyway, maybe a gem?, to pretty print hashes?
所以它看起来像这样:
{
entry1 => {
entrey1.1 => 1,
entry1.2 => 3
},
entry2 => 3
}
而不是:{ entry1 =>{ 入口1.1 =>1、entry1.2 =>3}, entry2 =>3 }代码>?
谢谢!
推荐答案
你可以使用 awesome_print gem.
you could use the awesome_print gem for that.
https://github.com/michaeldv/awesome_print
require 'awesome_print' # if you like to have it in irb by default, add it to your irbrc
>> ap({:a => 1, :b => [1,2,3], :c => :d})
{
:b => [
[0] 1,
[1] 2,
[2] 3
],
:a => 1,
:c => :d
}
顺便说一句,您也可以使用 p object
代替 puts object.inspect
,它在打印之前在对象中调用检查.另一种打印对象比默认 puts 更好的方法是使用 ruby stdlib 中的 pp
( http://ruby-doc.org/stdlib/libdoc/pp/rdoc/index.html)
btw, instead of puts object.inspect
you can also just use p object
which calls inspect in the object before printing it.
another way to print objects a little nicer than the default puts is to use pp
from the ruby stdlib ( http://ruby-doc.org/stdlib/libdoc/pp/rdoc/index.html )
这篇关于Ruby on Rails:variable.hash_set.inspect 的漂亮打印...有没有办法在控制台中漂亮地打印 .inpsect?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!