漂亮打印散列的最佳方法 [英] Best way to pretty print a hash
问题描述
我有一个带有嵌套数组和散列的大散列.我想简单地将其打印出来,以便用户可读".
I have a large hash with nested arrays and hashes. I would like to simply print it out so it 'readable' to the user.
我希望它有点像 to_yaml - 这很好读 - 但看起来仍然太技术化.
I would like it to be sort of like to_yaml - that's pretty readable - but still too tech looking.
最终将是需要读取这些数据块的最终用户,因此需要对其进行干净的格式化.
Ultimately its going to be end users who need to read these data chunks so they need to be formatted cleanly.
有什么建议吗?
推荐答案
require 'pp'
pp my_hash
使用pp
如果你需要一个内置的解决方案并且只想要合理的换行符.
Use pp
if you need a built-in solution and just want reasonable line breaks.
如果您可以安装 gem,请使用 awesome_print.(根据您的用户,您可能希望使用 index:false
选项关闭显示数组索引.)
Use awesome_print if you can install a gem. (Depending on your users, you may wish to use the index:false
option to turn off displaying array indices.)
这篇关于漂亮打印散列的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!