2009/8/10 Brian Candler <b.candler / pobox.com>:
> 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 =3D {}
> File.open("conf.ini") do |source|
> =A0source.each_line do |line|
> =A0 =A0if line =3D~ /^(\w+)\s*=3D\s*"(.*)"\s*$/
> =A0 =A0 =A0conf[$1] =3D $2
> =A0 =A0end
> =A0end
> end
>
> Here I build a Hash, so you can access it as
>
> =A0conf['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 =3D {}
File.foreach "conf.ini" do |line|
  conf[$1] =3D $2 if /^\s*(\w+)\s*=3D\s*"([^"]*)"\s*$/ =3D~ line
end

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/