From: "Mauricio Fernández" Date: 2003-04-03T02:34:44+09:00 Subject: Re: eval'ing a config file 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 ;-): >> Object.const_set("AA", 12) => 12 >> AA => 12 -- _ _ | |__ __ _| |_ ___ _ __ ___ __ _ _ __ | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ | |_) | (_| | |_\__ \ | | | | | (_| | | | | |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_| Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com Turn right here. No! NO! The OTHER right!