How do I get Ruby awesome_print to file?
Asked Answered
U

1

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  
Undersecretary answered 13/2, 2015 at 23:42 Comment(0)
C
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: https://mcmap.net/q/1261992/-outputting-stdout-to-a-file-and-back-again awesome_print doesn't seem to return the value, so no assigning it to a variable :(
Capitate answered 14/2, 2015 at 5:0 Comment(2)
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)Polyhydric
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.Greg

© 2022 - 2024 — McMap. All rights reserved.