Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pretty print to a file in ruby

Tags:

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 ?

like image 285
codeObserver Avatar asked Feb 10 '12 21:02

codeObserver


People also ask

How do you make a pretty print in Ruby?

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.

Which method is used writing to the file in Ruby?

The content “File Handling” is written to the file using syswrite method.

What are the ruby file open modes?

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.


1 Answers

require 'pp'  File.open("test.txt","w") do |f|   PP.pp(self,f) end 
like image 140
Huy Le Avatar answered Oct 10 '22 09:10

Huy Le