8

I am trying to get awesome_print to output to a file rather than the console but I cant find out how to do this?

require "awesome_print"

mySymbolizedHash = {'blah' => 'blabbbb', 'this' => 'that'}

This will write to console, I need to write the formatted output to file.

If I write the hash directly to a file, its not formatted they way I want.

ap mySymbolizedHash  
Freedom_Ben
  • 11,247
  • 10
  • 69
  • 89

1 Answers1

17
File.open('some_file', 'w') do |f|
  f.write mySymbolizedHash.awesome_inspect
end

awesome_inspect seems undocumented, but ai seems to be an alias, and that's used all over the place.

You could redirect STDOUT to a file, as shown here: http://stackoverflow.com/questions/1470344/outputting-stdout-to-a-file-and-back-again awesome_print doesn't seem to return the value, so no assigning it to a variable :(
Satya
  • 4,458
  • 21
  • 29
  • 5
    if you don't want the color codes and array indices in the output file, you can add these options `f.write mySymbolizedHash.awesome_inspect(:plain => true, :index => false)` – Tilo Mar 23 '16 at 18:33
  • 1
    If it's not obvious, `str = my_hash.awesome_inspect(:plain => true, :index => false)` saves awesome-print's output to a string, which may be useful if some post-processing is required before printing. – Cary Swoveland Aug 22 '16 at 21:30