Ruby: Remove instances of a method

Here’s an “old school” style jig. I first attempted this in Haskell, but I needed Perl-compatible regular expressions (Posix can’t handle the negative lookahead needed for correctly identifying C-style comments), and installing the PCRE library for Haskell on Windows is painful, though I’ll get around to it one of these days.

 
require 'find'
 
Find.find "Components\\CommComponents" do |p|
  Find.prune if p =~ /\.svn$/i
  next unless p =~ /\.(cpp|cxx|h)$/i
 
  data = File.open(p,"rb") {|f| f.read}
  orig_data = data.clone
  if p =~ /\.h$/
    data.gsub!(/
      \s*
      (?:^[\ \t]*\/\/[^\n]*\n)*
      ^[\ \t]*(?:virtual\s+)?HRESULT\s+(?:[A-Za-z0-9_]*::)?GetAttributes\s\([^\)]*\)\s*;[\ \t]*
      /mx, "")
  elsif p =~ /\.(cpp|cxx)$/
    data.gsub!(/
      \s*
      (?:\/\*(?:[^\*]|\*(?!\/))*\*\/)?\s*
      HRESULT\s+[A-Za-z_][A-Za-z0-9_]*::GetAttributes\s*\(\s*
        ATTRIBUTES\s*\*[^,)]*,\s*int\s*\*[^,)]*\)\s*
        \{[^\}]*\}
      /mx, "")
  end
  if data != orig_data
    File.open(p,"wb") {|f| f.write(data) }
  end
end
Posted by: Jason Felice on September 3, 2009 • Tags: , , • Posted in: Jigs

Comments are closed for this entry.