Ruby on Rails: pretty print for variable.hash_set.inspect ... is there a way to pretty print .inpsect in the console?
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, 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 )
Shortcut for showing list of hashes nicely
AS said in my comments, I like to use y hash
or puts YAML.dump(hash)
that shows your hash in yaml. It can be used for other objects too.
h = {:a => 1, :b => 2, :c => 3}
# => {:a=>1, :b=>2, :c=>3}
y h
#---
#:a: 1
#:b: 2
#:c: 3
# => nil
There is also an informative answer about it.
Related Topics
Make Rails Ignore Daylight Saving Time When Displaying a Date
How to Stringize/Serialize Ruby Code
Ruby on Rails 4 Select Multiple
Ruby.Metaprogramming. Class_Eval
Delete Contents of Array Based on a Set of Indexes
Using Nokogiri to Split Content on Br Tags
Dynamic Active Record Store Accessors Based Off a User Form
How to Correctly Install Rvm in Docker
Load Two Ruby Modules/Gems with the Same Name
Does Ruby Have Any Number Formatting Classes
How to Set a Hook to Run Code at the End of a Ruby Class Definition
Wrapping Text into Lines at Word Boundaries