From: Robert Klemme Date: 2004-08-22T07:15:54+09:00 Subject: Re: Ruby way to update file lines "Mark Probert" schrieb im Newsbeitrag news:Xns954C7C2A261F8probertmnospamtelusn@198.161.157.145... > "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. Thanks, too! :-) > One minor correction: COMMENT and DELETE should be lower case. Ruby 1.8 > spits and error about constants otherwise. They are meant to be constants because they *are* constant. I just put both variants into a single file but you'll likely need only one of them, don't you? Regards robert