From: Mark Probert Date: 2004-08-22T04:15:47+09:00 Subject: Re: Ruby way to update file lines "Robert Klemme" wrote: > > Some code thoughts below > > Regards > > robert > > > # match keys > COMMENT = lambda {|key, val| "# #{key}:#{val}"} > DELETE = lambda { nil } > > tasks = { > "foo:bar" => COMMENT, > "foo1:bar1" => DELETE, > } > > conf = File.readlines("conf.txt") > > # manipulate > conf.map! do |line| > if /^([^:]+:[^:]+):(.*)$/ =~ line && (fun = tasks[$1]) > fun.call $1, $2 > else > line > end > end > > # remove deleted lines > conf.compact! > > File.open("conf-1.txt", "w") {|io| io.puts conf } > I liked this one :-) Thanks. One minor correction: COMMENT and DELETE should be lower case. Ruby 1.8 spits and error about constants otherwise. -mark.