Skip to content

Instantly share code, notes, and snippets.

@Lumbe
Last active December 7, 2016 14:50
Show Gist options
  • Save Lumbe/f57c97c496570a69884b5e95b5f2b783 to your computer and use it in GitHub Desktop.
Save Lumbe/f57c97c496570a69884b5e95b5f2b783 to your computer and use it in GitHub Desktop.
Remove lines from file on regex match

How to remove lines from file on regex match in ruby

# remove_comment_lines_from_haml_file.rb
require 'fileutils'

open('file.html.haml', 'r') do |f|
  open('file.html.haml.tmp', 'w') do |f2|
    f.each_line do |line|
       f2.write(line) unless line =~ /^\s+\/.*$/ # regex for comments in haml template
    end
  end
end
FileUtils.mv 'file.haml.html.tmp', 'file.html.haml'
puts 'success'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment