Skip to content

Instantly share code, notes, and snippets.

@guicattani
Created May 24, 2021 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guicattani/bc1840e637a2f4038da24098b4b3f712 to your computer and use it in GitHub Desktop.
Save guicattani/bc1840e637a2f4038da24098b4b3f712 to your computer and use it in GitHub Desktop.
Find commented code in Ruby
#!/usr/bin/env ruby
ARGV.each do |filename|
fileobject = File.new(filename, "r")
next if fileobject.nil?
occurences = 0
lines = fileobject.readlines
lines.each do |line|
next unless line.match(/\A *#.+\n/)
if line.match(/.*[print|puts] ["'].*["']\n/)
occurences+=1
next
end
line = line.gsub(/\n/,"")
line = line.sub(/\A *# */," ")
begin
eval(line)
rescue SyntaxError => e
occurences+=1 if e.message.match(/.*unexpected end.*|.*void.*/)
next
rescue
occurences+=1
next
end
next if line.match(/ *#.*/) # double comments
occurences+=1
end
next if occurences == 0
percentage = (occurences/Float(lines.count)).truncate(4) * 100
next if percentage < 10
pp "#{fileobject.path} has ~#{occurences}/#{lines.count}(#{percentage}%) lines with commented code"
fileobject.close()
local_variables.each { |e| eval("#{e} = nil") }
end
@guicattani
Copy link
Author

guicattani commented May 24, 2021

Quick disclaimer, this script is by no means exhaustive!

You can run it with find */ -name *.rb -type f | xargs ruby ./find_commented_code.arb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment