I am trying to pretty print a hash to a file.
我想把散列打印到文件中。
I tried unix redirects [added different flags to it incrementally] :
我尝试了unix重定向[递增地添加不同的标志]:
`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 ?
还有,必须有一种更简单的方法在ruby中以一行写文件?而不是做文件。新的然后写入文件?
7 个解决方案
#1
43
require 'pp'
File.open("test.txt","w") do |f|
PP.pp(self,f)
end
#2
2
The use of backticks here is perplexing since those are used for executing shell commands.
这里反勾的使用令人费解,因为它们用于执行shell命令。
What you probably mean is:
你的意思是:
File.open(@dir_+"/myfile.out",'w+') do |f|
f.write(pp(get_submap_from_final(all_mapping_file,final_map)))
end
The pp
method always writes to the console so you might see it and still have it written.
pp方法总是写到控制台,所以您可能会看到它,并且仍然要写它。
#3
1
What about (not using pp directly):
关于(不直接使用pp):
File.open("myfile.out","w+") do |f|
f.puts mymap.inspect
end
Or even redirect stdout for the file
甚至为文件重定向stdout
file = File.open("myfile.out", "w+)
old_stdout = STDOUT
$stdout = STDOUT = file
pp get_submap_from_final(all_mapping_file,final_map)
$stdout = STDOUT = old_stdout
#4
0
Doing something similar to what Edu suggested and How do I redirect stderr and stdout to file for a Ruby script? helped.
做一些类似于Edu所建议的事情,以及如何重定向stderr和stdout文件以获取Ruby脚本?帮助。
Here is how the new code is similar to:
新守则的内容如下:
$stdout.reopen(@dir_+"/my_file.out",'w+')
puts "All metrics are:"
pp final_map
$stdout=STDOUT
Would still be interested to know why redirection operators > , and 2>&1 in back-ticks doesn't work
还会有兴趣知道为什么重定向操作>和2>&1的反节拍不起作用吗
#5
0
Given the mymap
from the question, you can use the "show_data" gem, which provides two methods: show_data
and format_data
. The latter produces a "pretty-printed" string of its argument which can then be fed to any output method.
给定问题中的mymap,您可以使用“show_data”gem,它提供了两个方法:show_data和format_data。后者生成一个“漂亮打印”的参数字符串,然后可以将其输入到任何输出方法。
require 'show_data'
$stderr.puts format_data(mymap)
For example:
例如:
myhash = { 'owners' => 21050, 'users' => 16877, 'portfolios' => 583,
'properylists' => 0, 'properties' => 29504, 'units' => 62688,
'tenants' => 85856 }
$stderr.puts format_data(myhash)
on STDERR:
STDERR:
{ 'owners' => 21050,
'users' => 16877,
'portfolios' => 583,
'properylists' => 0,
'properties' => 29504,
'units' => 62688,
'tenants' => 85856
}
#6
-1
require 'pp'
class File
def pp(*objs)
objs.each {|obj|
PP.pp(obj, self)
}
objs.size <= 1 ? objs.first : objs
end
end
File.open('output','w') do |file|
file.pp mymap
end
#7
-1
Here's an expansion to the post above to also to pretty print json output to a file.
这是对上面文章的扩展,也可以将json输出打印到文件中。
require "pp"
require "json"
class File
def pp(*objs)
objs.each {|obj|
PP.pp(obj, self)
}
objs.size <= 1 ? objs.first : objs
end
def jj(*objs)
objs.each {|obj|
obj = JSON.parse(obj.to_json)
self.puts JSON.pretty_generate(obj)
}
objs.size <= 1 ? objs.first : objs
end
end
test_object = { :name => { first: "Christopher", last: "Mullins" }, :grades => [ "English" => "B+", "Algebra" => "A+" ] }
test_json_object = JSON.parse(test_object.to_json)
File.open("log/object_dump.txt", "w") do |file|
file.pp(test_object)
end
File.open("log/json_dump.txt", "w") do |file|
file.jj(test_json_object)
end
#1
43
require 'pp'
File.open("test.txt","w") do |f|
PP.pp(self,f)
end
#2
2
The use of backticks here is perplexing since those are used for executing shell commands.
这里反勾的使用令人费解,因为它们用于执行shell命令。
What you probably mean is:
你的意思是:
File.open(@dir_+"/myfile.out",'w+') do |f|
f.write(pp(get_submap_from_final(all_mapping_file,final_map)))
end
The pp
method always writes to the console so you might see it and still have it written.
pp方法总是写到控制台,所以您可能会看到它,并且仍然要写它。
#3
1
What about (not using pp directly):
关于(不直接使用pp):
File.open("myfile.out","w+") do |f|
f.puts mymap.inspect
end
Or even redirect stdout for the file
甚至为文件重定向stdout
file = File.open("myfile.out", "w+)
old_stdout = STDOUT
$stdout = STDOUT = file
pp get_submap_from_final(all_mapping_file,final_map)
$stdout = STDOUT = old_stdout
#4
0
Doing something similar to what Edu suggested and How do I redirect stderr and stdout to file for a Ruby script? helped.
做一些类似于Edu所建议的事情,以及如何重定向stderr和stdout文件以获取Ruby脚本?帮助。
Here is how the new code is similar to:
新守则的内容如下:
$stdout.reopen(@dir_+"/my_file.out",'w+')
puts "All metrics are:"
pp final_map
$stdout=STDOUT
Would still be interested to know why redirection operators > , and 2>&1 in back-ticks doesn't work
还会有兴趣知道为什么重定向操作>和2>&1的反节拍不起作用吗
#5
0
Given the mymap
from the question, you can use the "show_data" gem, which provides two methods: show_data
and format_data
. The latter produces a "pretty-printed" string of its argument which can then be fed to any output method.
给定问题中的mymap,您可以使用“show_data”gem,它提供了两个方法:show_data和format_data。后者生成一个“漂亮打印”的参数字符串,然后可以将其输入到任何输出方法。
require 'show_data'
$stderr.puts format_data(mymap)
For example:
例如:
myhash = { 'owners' => 21050, 'users' => 16877, 'portfolios' => 583,
'properylists' => 0, 'properties' => 29504, 'units' => 62688,
'tenants' => 85856 }
$stderr.puts format_data(myhash)
on STDERR:
STDERR:
{ 'owners' => 21050,
'users' => 16877,
'portfolios' => 583,
'properylists' => 0,
'properties' => 29504,
'units' => 62688,
'tenants' => 85856
}
#6
-1
require 'pp'
class File
def pp(*objs)
objs.each {|obj|
PP.pp(obj, self)
}
objs.size <= 1 ? objs.first : objs
end
end
File.open('output','w') do |file|
file.pp mymap
end
#7
-1
Here's an expansion to the post above to also to pretty print json output to a file.
这是对上面文章的扩展,也可以将json输出打印到文件中。
require "pp"
require "json"
class File
def pp(*objs)
objs.each {|obj|
PP.pp(obj, self)
}
objs.size <= 1 ? objs.first : objs
end
def jj(*objs)
objs.each {|obj|
obj = JSON.parse(obj.to_json)
self.puts JSON.pretty_generate(obj)
}
objs.size <= 1 ? objs.first : objs
end
end
test_object = { :name => { first: "Christopher", last: "Mullins" }, :grades => [ "English" => "B+", "Algebra" => "A+" ] }
test_json_object = JSON.parse(test_object.to_json)
File.open("log/object_dump.txt", "w") do |file|
file.pp(test_object)
end
File.open("log/json_dump.txt", "w") do |file|
file.jj(test_json_object)
end