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?
问题描述
我发现自己在我的功能测试中做了很多的.inpsect s,以确保我知道数据是如何格式化的...但是当哈希对象中的每个条目后面没有新行时,散列很难读取。
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 = {entrey1.1 => 1,entry1.2 => 3},entry2 => 3}
?
instead of: { entry1 => { entrey1.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
}
btw,而不是 puts object.inspect
也可以使用 p 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屋!