使用Thor删除文件中的一行

use*_*206 3 ruby-on-rails thor

是否可以使用Thor操作从文件中删除一行文本.例如,我想删除ruby评论.到目前为止,我已经找到了两个动作:comment_lines- 注释掉行,和gsub_file

谢谢

psc*_*egr 5

这不能删除评论吗?

gsub_file(filename, /#.*$/, '') 
Run Code Online (Sandbox Code Playgroud)

编辑:

如果您想删除评论并删除仅包含评论信息的行,您可以尝试:

gsub_file(filename, /\S*#.*$/, '')   # removes partial comments
gsub_file(filename, /^\s*#.*\n/, '') # removes full line comments
Run Code Online (Sandbox Code Playgroud)