【发布时间】:2014-03-12 12:33:24
【问题描述】:
我正在用 Ruby 编写一个从 Ruby 文件中删除 cmets 的小脚本:
#!/usr/bin/ruby
def uncomment(file)
File.readlines(file).each do |line|
if line =~ /(^\s*#|^\t*#)(?!\!).*/
puts line + " ==> this is a comment"
#todo: remove line from the file
end
end
end
puts "Fetching all files in current directory and uncommenting them"
# fetching all files
files = Dir.glob("**/**.rb")
# parsing each file
files.each do |file|
#fetching each line of the current file
uncomment file
end
我被困在如何删除与#todo 部分中的正则表达式匹配的这些行,如果有人可以提供帮助,那就太好了!
【问题讨论】:
-
是将结果写入新文件还是修改正在迭代的文件?
-
是修改当前文件。
-
访问这个 SO 答案@stackoverflow.com/questions/16638667/…
-
line.gsub!() 我猜应该也可以。
-
嗯,正则表达式还匹配在同一行中有代码和注释的行。当您删除这些行时,您的代码可能会无效。