Skip to content

Instantly share code, notes, and snippets.

@redraiment
Last active October 10, 2015 07:27
Show Gist options
  • Save redraiment/3655563 to your computer and use it in GitHub Desktop.
Save redraiment/3655563 to your computer and use it in GitHub Desktop.
Removes comments with C style (`//' and `/* */') in Ruby
require 'strscan'
lex = [/"[^\\"]*(?:\\.[^\\"]*)*"/, # string
/'[^\\']*(?:\\.[^\\']*)*'/, # char
/\/\*.*?\*\//m, # multi-line
/\/\/(?:.*?\\(?:\r?\n|\r))*.*/, # single-line
/.|\s+/] # rest
ARGV.each do |source|
stream = StringScanner.new File.read source
until stream.eos? do
code = stream.scan lex.find {|regex| stream.match? regex}
print code unless code.start_with?('//') || code.start_with?('/*')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment