Ruby: How to make IRB print structure for Arrays and Hashes
Another thing you can do is use the y
method which converts input into Yaml. That produces pretty nice output...
>> data = { 'dog' => 'Flemeale', 'horse' => 'Gregoire', 'cow' => 'Fleante' }=> {"cow"=>"Fleante", "horse"=>"Gregoire", "dog"=>"Flemeale"}>> y data--- cow: Fleantehorse: Gregoiredog: Flemeale
The pretty print works well, but the Awesome_Print gem is even better! You will have to require awesome_print
but it handles nested hashes and arrays beautifully plus colors them in the Terminal using 'ap' instead of 'p' to puts the output.
You can also include it in your ~/.irbrc to have this as the default method for displaying objects:
require "awesome_print"AwesomePrint.irb!