我正在尝试使用 Ruby 脚本复制文件,我有一个包含文件内容的哈希,然后我使用JSON.pretty_generate
将生成的文件以与原始文件相同的格式放置,但是间距不同。
这是原始文件:
{
"ip-fileinput-collector": {
"parameters": {
"delete_file": false,
"feed": "FileCollector",
"provider": "MyFeed",
"path": "/home/mf370/Desktop/",
"postfix": "teste_ip.csv",
"rate_limit": 300,
"enabled": true
},
"group": "Collector",
"name": "Fileinput",
"module": "collector_file",
"description": "Fileinput collector fetches data from a file."
}
}
这是我的程序的输出:
{
"ip-fileinput-collector": {
"parameters": {
"delete_file": false,
"feed": "FileCollector",
"provider": "MyFeed",
"path": "/home/mf370/Desktop/",
"postfix": "teste_ip.csv",
"rate_limit": 300,
"enabled": true
},
"group": "Collector",
"name": "Fileinput",
"module": "collector_file",
"description": "Fileinput collector fetches data from a file."
}
}
我拥有的代码是:
file_content = {"ip-fileinput-collector"=>{"parameters"=>{"delete_file"=>false, "feed"=>"FileCollector", "provider"=>"MyFeed", "path"=>"/home/mf370/Desktop/", "postfix"=>"teste_ip.csv", "rate_limit"=>300, "enabled"=>true}, "group"=>"Collector", "name"=>"Fileinput", "module"=>"collector_file", "description"=>"Fileinput collector fetches data from a file."}
require 'json'
File.open('my_file.conf','w+') do |f|
f.write(JSON.pretty_generate(file_content))
end
Ruby 中复制原始文件并具有相同间距的最佳方法是什么?