Skip to content

Instantly share code, notes, and snippets.

@stephenlauck
Created February 28, 2014 17:35
Show Gist options
  • Save stephenlauck/9275648 to your computer and use it in GitHub Desktop.
Save stephenlauck/9275648 to your computer and use it in GitHub Desktop.
Convert INI file to Hash in Ruby
#!/usr/bin/env ruby
require 'pp'
ini = Hash.new
cur_section = nil
File.open("Test.ini").each do |line|
if line.strip.split(';').first =~ /^\[(.*)\]$/
cur_section = $1
ini[cur_section] = Hash.new
next
end
if line.strip.split(';').first =~ /\=/
key = line.strip.split(';').first.split('=')
ini[cur_section].merge!( key[0] => key[1].nil? ? '' : key[1] )
end
end
pp ini
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment