diff --git a/README.md b/README.md index edd67d3..afbe13c 100644 --- a/README.md +++ b/README.md @@ -16,12 +16,24 @@ More information about INI files can be found on the [Wikipedia Page](http://en. ### Properties -The basic element contained in an INI file is the property. Every property has +A basic element contained in an INI file is the property. Every property has a name and a value, delimited by an equals sign *=*. The name appears to the left of the equals sign and the value to the right. name=value +### Switches + +Switches are sometimes present in INI files to indicate whether a feature +should be on or off. Switches are made up by a single word on a line on its +own. + + name + +Switches are represented internally as an empty hash `{}` to avoid clashing +with other values and are deactivated by removing the line from the file +completely. + ### Sections Section declarations start with *[* and end with *]* as in `[section1]` and @@ -49,6 +61,7 @@ A typical INI file might look like this: # another comment var1 = baz var2 = shoodle + a_switch Implementation diff --git a/lib/inifile.rb b/lib/inifile.rb index fbcfb0e..4df319e 100644 --- a/lib/inifile.rb +++ b/lib/inifile.rb @@ -98,7 +98,13 @@ def write( opts = {} ) File.open(filename, mode) do |f| @ini.each do |section,hash| f.puts "[#{section}]" - hash.each {|param,val| f.puts "#{param} #{@param} #{escape_value val}"} + hash.each {|param,val| + if val.class == Hash and val.empty? + f.puts param + else + f.puts "#{param} #{@param} #{escape_value val}" + end + } f.puts end end @@ -428,6 +434,7 @@ def initialize( hash, param, comment, default ) @section_regexp = %r/\A\s*\[([^\]]+)\]#{comment}/ @ignore_regexp = %r/\A#{comment}/ @property_regexp = %r/\A(.*?)(? "test/data/tmp.ini") + ini_file["foo"] = {"switch_support" => {}} + ini_file.save + + assert File.read("test/data/tmp.ini") =~ /^switch_support$/ + end end