【问题标题】:deleting a line in a file using Thor使用 Thor 删除文件中的一行
【发布时间】:2013-02-08 08:31:47
【问题描述】:

是否可以使用 Thor 操作从文件中删除一行文本。例如,我想删除 ruby​​ cmets。 到目前为止,我已经找到了 2 个操作:comment_lines - 其中 cmets out 行,以及 gsub_file

谢谢

【问题讨论】:

    标签: ruby-on-rails thor


    【解决方案1】:

    这对删除 cmets 不起作用吗?

    gsub_file(filename, /#.*$/, '') 
    

    编辑:

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

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

    【讨论】:

    • 谢谢,我想到了这一点,但我认为这样做的负面影响是,我现在会在曾经有 cmets 的地方出现空行。理想情况下,我希望尽可能少的多余行。
    • 嗯...我以为第二个也会杀死\n
    • @pschuegr 确实如此;第二个为我完全删除了这条线。
    猜你喜欢
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-21
    • 2014-04-22
    • 1970-01-01
    相关资源
    最近更新 更多