how break out of scope ruby variable ruby -
i have used following code delete lines file:
file.open("#{$whitelist}-tmp", "w") |outfile| file.foreach("#{$whitelist}") |li| outfile.puts li unless li.chomp[/\a#{instanceid}\z/] @comm = outfile end end fileutils.mv(@comm, $whitelist)
it works, i'm thinking it's not right way it, had create variable '@comm' communicate filename out loop. if used 'outfile' first argument of fileutils.mv directly got undefined variable error.
so, how should have done this?
thanks in advance.
the simplest way assign variable containing filename before block use it:
tempfile = "#{$whitelist}-tmp" file.open(tempfile, "w") |outfile| file.foreach("#{$whitelist}") |li| outfile.puts li unless li.chomp[/\a#{instanceid}\z/ end end fileutils.mv(tempfile, $whitelist)
Comments
Post a Comment