Created
May 24, 2021 13:42
-
-
Save guicattani/bc1840e637a2f4038da24098b4b3f712 to your computer and use it in GitHub Desktop.
Find commented code in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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