From: voipfc@... Date: 2006-10-01T17:50:17+09:00 Subject: Re: Ruby's equivalent of PHP explode Ryan Eibling wrote: > Paul Lutus wrote: > > > > data = File.read("filename.ini") > > > > my_hash = {} > > I am trying to understand what {} means in ruby. I understand that it means an empty block, but what does it mean in this context? Does it mean nil? Does it simply my_hash as an associative array? > > 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']