I am trying to pretty print a hash to a file.
I tried unix redirects [added different flags to it incrementally] :
`echo #{pp mymap} | tee summary.out 2>&1`
and File IO
my_file = File.new(@dir_+"/myfile.out",'w+') my_file.puts `#{pp get_submap_from_final(all_mapping_file,final_map)}`
It always prints to console and doesnt write to a file.
Also there has to be an easier way to write to file in one line in ruby ? instead of doing File.new and then writing to a file ?
To define a customized pretty printing function for your classes, redefine method #pretty_print(pp) in the class. #pretty_print takes the pp argument, which is an instance of the PP class. The method uses text , breakable , nest , group and pp to print the object.
The content “File Handling” is written to the file using syswrite method.
Ruby allows the following open modes: "r" Read-only, starts at beginning of file (default mode). "r+" Read-write, starts at beginning of file. "w" Write-only, truncates existing file to zero length or creates a new file for writing.
require 'pp' File.open("test.txt","w") do |f| PP.pp(self,f) end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With