Thursday, 5 September 2013

How do I insert a 'section' at the top of IniFile via Ruby

How do I insert a 'section' at the top of IniFile via Ruby

I have an Ini file, and I am using IniFile to help parse it. I want to add
a new section at the top of the ini file, how do I do this?



sample.ini
[Student1]
A = 1
[Student2]
B = 1



My expected result,
[Summary]
NumOfStudents=2
[Student1]
A = 1
[Student2]
B = 1



Problem: the Summary section is always inserted at bottom.
[Student1]
A = 1
[Student2]
B = 1
[Summary]
NumOfStudents=2



My Ruby script
iniFileHandle = IniFile.load(sample.ini)
numOfStudents = iniFileHandle.sections.size
iniFileHandle['Summary'] = {'NumOfStudents' => numOfStudents}



Additional Ruby IniFile 2.0.2 Doc From the doc, I don't find any functions
to insert a section at the top.

No comments:

Post a Comment