From: Ryan Eibling Date: 2006-09-27T01:45:17+09:00 Subject: Re: Ruby's equivalent of PHP explode Paul Lutus wrote: > > data = File.read("filename.ini") > > my_hash = {} > > data.each do |line| > key,value = line.split("=") > my_hash[key] = value > end Here's a modified version that supports sections: data = File.read("filename.ini") ini_hash = {} ini_hash[""] = section_hash = {} # default unnamed section data.each do |line| if /^\[.+\]$/ =~ line # section headers are surrounded by brackets ini_hash[line.chomp.gsub(/[\[\]]/, '')] = section_hash = {} else key,value = line.chomp.split("=") section_hash[key] = value end end # example of walking through the whole thing ini_hash.each do |k, v| puts "-#{k}-" v.each {|sk, sv| puts " #{sk}=#{sv}"} end # example of accessing a value by section and key v = ini_hash['section']['key']