1) Why is the order of the sections in an ini file relevant?
2) my_prog.rb:
require 'inifile'
ini_file = IniFile.load("sample.ini")
numOfStudents = ini_file.sections.size
h = {
"Summary" => { "NumOfStudents" => numOfStudents }
}
ini_file.instance_eval do
p @ini #instance_eval() changes self to ini_file, and
h.merge! @ini #all instance variables are looked up in whatever
@ini = h #object is self. If you look at the inifile gem's source
end #code, the sections of the ini file are stored in a
#hash called @ini
ini_file.write
--output:--
$ cat sample.ini
[Student1]
A = 1
[Student2]
B = 1
~/ruby_programs$ ruby my_prog.rb
{"Student1"=>{"A"=>"1"}, "Student2"=>{"B"=>"1"}}
~/ruby_programs$ cat sample.ini
[Summary]
NumOfStudents = 2
[Student1]
A = 1
[Student2]
B = 1
3) Or you can do it this way:
require 'inifile'
class IniFile
attr_accessor :ini
end
ini_file = IniFile.load("sample.ini")
numOfStudents = ini_file.sections.size
h = {
"Summary" => { "NumOfStudents" => numOfStudents }
}
results = h.merge! ini_file.ini
ini_file.ini = results
ini_file.write