漂亮打印到红宝石文件 [英] pretty print to a file in ruby

查看:69
本文介绍了漂亮打印到红宝石文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将哈希值漂亮地打印到文件中.

I am trying to pretty print a hash to a file.

我尝试了unix重定向[向其逐步添加了不同的标志]:

I tried unix redirects [added different flags to it incrementally] :

`echo #{pp  mymap} | tee summary.out 2>&1`

和文件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.

还必须有一种更简单的方法来在ruby中一行写入文件吗?而不是执行File.new然后写入文件?

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 ?

推荐答案

require 'pp'

File.open("test.txt","w") do |f|
  PP.pp(self,f)
end

这篇关于漂亮打印到红宝石文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆