TwP / inifile

Native Ruby package for reading and writing INI files
http://codeforpeople.rubyforge.org/inifile
95 stars 47 forks source link

Add array syntax support #24

Closed iongion closed 10 years ago

iongion commented 10 years ago

Would you be willing to add PHP like array syntax support for reading/generating ini files, such as:

[section] val[]=1 val[]="stuf" val[]="bla"

  def write( opts = {} )
    filename = opts.fetch(:filename, @filename)
    encoding = opts.fetch(:encoding, @encoding)
    mode = (RUBY_VERSION >= '1.9' && encoding) ?
         "w:#{encoding.to_s}" :
         'w'
    File.open(filename, mode) do |f|
      @ini.each do |section,hash|
        f.puts "[#{section}]"
        hash.each { |param,val| 
          if val.kind_of?(Array)
            val.each { |x| 
              f.puts "#{param}[] #{@param} #{escape_value x}"
            }
          else
            f.puts "#{param} #{@param} #{escape_value val}"
          end
        }
        f.puts
      end
    end
    self
  end
TwP commented 10 years ago

The INI file format is very basic and designed to support key / value pairs located in various sections. If you really need more complex types such as arrays I would look into YAML or JSON for the storage format.

I'm going to close this one out as "won't implement". There is a deep rabbit hole here that I really don't want to go down.

garrylachman commented 6 years ago

Array datatype in INI files are very common, in my case i use this lib in chef cookbook and some server use this format, moving to different format is not an option.

Some examples: http://php.net/manual/en/function.parse-ini-file.php https://www.npmjs.com/package/ini-config-parser