From: Bruce Williams Date: 2003-04-03T11:52:31+09:00 Subject: Re: eval'ing a config file On Wednesday 02 April 2003 12:34 pm, Mauricio Fern�ndez wrote: | On Wed, Apr 02, 2003 at 02:15:15AM +0900, Ian Macdonald wrote: | > Hi, | > | > I have a number of LDAP-related programs that I want to share a common | > config file in which certain variables are stored, such as server, port, | > password, etc. in the following form. | > | > LDAP_HOST = foo.bar.com | > LDAP_PORT = 389 | > LDAP_PASS = foobar | > | > I want the parser to ignore blank lines and those that start with a '#'. | > | > The code that I've come up with is this: | > | > File.open("/etc/ldaprc") do |f| | > f.each do |line| | > next if line =~ /^(#|$)/ | > var, val = /^(.+?)\s*=\s*(.+)$/.match(line)[1,2] | > eval "#{var} = '#{val}'" | > end | > end | > | > It works, but looking at it, it looks too much like code I might have | > written for Perl some years ago. Is there a more Rubyesque way to do | > this? In particular, is there a cleaner way than eval? | | Just for the eval part (IMHO the whole problem would be handled better | | by some module in the RAA ;-): Personally, I'd sacrifice the equals sign, make it a colon, and call it a yaml file. YAML would then take care of the primitives. You could even iterate through and create constants if you don't feel like using a configuration hash, or create a type to handle it upon loading. You'd even have things like aliases/anchors, without having to homebrew them. A matter of taste, I suppose. // Bruce