From: Robert Klemme Date: 2009-08-10T22:51:27+09:00 Subject: Re: Reading contents of a file and storing 2009/8/10 Brian Candler : > A lot of people use YAML, a different format like this: > > url: http://localhost > emailid: User@htmdec.com > password: "12345678" > browser: iexplore > > However personally I dislike YAML because it's very data-sensitive (note > the need for quotes around the password above) and sometimes buggy. > > If you google for "ruby ini file" you'll find some code which probably > does what you need. Otherwise, it's pretty easy to roll your own: > > conf = {} > File.open("conf.ini") do |source| >  source.each_line do |line| >    if line =~ /^(\w+)\s*=\s*"(.*)"\s*$/ >      conf[$1] = $2 >    end >  end > end > > Here I build a Hash, so you can access it as > >  conf['url'] > > But you could build an OpenStruct instead (look at ostruct.rb) so you > could access it as conf.url You can save one level of indentation by using File.foreach "conf.ini" do |line| ... end And if you want to get really short (not necessarily recommended): conf = {} File.foreach "conf.ini" do |line| conf[$1] = $2 if /^\s*(\w+)\s*=\s*"([^"]*)"\s*$/ =~ line end Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/