Skip to content

Instantly share code, notes, and snippets.

@piotrze
Last active August 29, 2015 14:22
Show Gist options
  • Save piotrze/fdecd604741aa8e85ffa to your computer and use it in GitHub Desktop.
Save piotrze/fdecd604741aa8e85ffa to your computer and use it in GitHub Desktop.
Find unused methods in your ruby (ruby on rails) projects
#!/usr/bin/env ruby
skip_methods = %w(new create edit update destroy index show initialize to_s call as_json page per_page build up down change)
output = `grep -hr "^\s*def\s" app/`
methods = output.split("\n")
puts "Found #{methods.size} methods."
unused_methods = []
methods.each_with_index do |line, i|
puts "\n>> #{i} <<\n" if i % 100 == 0
line = line.gsub(/\A\s*(.*?)\s*\z/, '\1')
.gsub(/\(.*/, '')
.gsub(/def /, '')
.gsub(/self\./, '')
next if skip_methods.include?(line)
next if line[-1] == '='
print '.'
if `grep -hr "#{line}" app/`.split("\n").size == 1
puts "\nCheck up on: #{line}\n"
unused_methods << line
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment