0

I'm trying to replicate a file with a Ruby Script, I have an Hash with the content of the file and then I use JSON.pretty_generate to put the file I generate on the same format as the original file, however the spacing is different.

This is the original 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."
    }
}

And this is the output from my program:

{
  "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."
  }
}

And the code that I have is:

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

What is the best way in Ruby to replicate the original file and have the same spacing?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
mf370
  • 321
  • 3
  • 14

0 Answers0