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.
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.
Any suggestions?
require 'pp'
pp my_hash
Use pp
if you need a built-in solution and just want reasonable line breaks.
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.)
If you have JSON, I recommend JSON.pretty_generate(hash)
because it is simpler than awesome_print, looks great in a pre
tag, and allows for easy copying from a web page. (See also: How can I "pretty" format my JSON output in Ruby on Rails?)
另一种比pp
或awesome_print
更适合我的解决方案:
require 'pry' # must install the gem... but you ALWAYS want pry installed anyways
Pry::ColorPrinter.pp(obj)
链接地址: http://www.djcxy.com/p/47152.html
下一篇: 最好的方式来漂亮打印散列