From: Robert Klemme Date: 2004-08-22T19:30:46+09:00 Subject: Re: Ruby way to update file lines "Mark Probert" schrieb im Newsbeitrag news:Xns954CA02966DD9probertmnospamtelusn@198.80.55.250... > "Robert Klemme" wrote in > news:2opvm8FddmmgU1@uni-berlin.de: > > > > 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? > > > When I leave them as constants, Ruby complains with the following error: > > deln.rb:37: dynamic constant assignment > COMMENT = lambda {|key, val| "# #{key}:#{val}" } > ^ Then you got the scoping wrong. Constants should be defined on top level or class / module scope, otherwise they are of not much use: what do you gain by reevaluationg and assigning an expression on each method invocation? That's not what constants are intended for. You probably did something like this: >> def foo() FOO = "bar" end SyntaxError: compile error (irb):2: dynamic constant assignment def foo() FOO = "bar" end ^ from (irb):2 While it should be FOO = "bar" def foo() ... end # use FOO or class Any FOO = "bar" # use FOO end Both of them don't trigger warnings unless you do multiple assignments to the same constant: >> FOO = "bar" => "bar" >> FOO = "bar" (irb):2: warning: already initialized constant FOO => "bar" > In the end, it worked out well. My code snippet looks like: > > # create a function map for the items we want to delete > comment = lambda {|key, val| "# #{key}:#{val}" } > delete = lambda {|key, val| nil } > checkmap = {} > @nodelist.each do |n| > key = "#{n.name}:#{n.ip}" > checkmap[key] = (@remove) ? delete : comment > end > > Where I now have the option of making the deletion permanent or just > commenting it out. Which is perfect for my application. Fine. robert