Skip to content

Instantly share code, notes, and snippets.

@blatyo
Created March 5, 2014 13:57
Show Gist options
  • Select an option

  • Save blatyo/9367609 to your computer and use it in GitHub Desktop.

Select an option

Save blatyo/9367609 to your computer and use it in GitHub Desktop.
class TallyBag
include Enumerable
def initialize
@tallys = Hash.new{0}
end
def each
if block_given?
@tallys.each(&Proc.new)
else
@tallys.each
end
end
def []=(obj, tally)
@tallys[obj] = tally
end
def [](obj)
@tallys[obj]
end
def <<(obj)
@tallys[obj] += 1
end
end
class UsageReporter
def initialize(files = Dir['**/*.rb'])
@files = files
@methods = Set.new
@total_number_of_calls = TallyBag.new
@file_number_of_calls = Hash.new{TallyBag.new}
end
def report
@files.each do |file|
extract_methods(file) do |body, method|
@file_number_of_calls[file][method] += body.scan(/\b#{method}\b/).size
@methods << method
end
end
@files.each do |file|
@methods.each do |method|
body = File.read(file)
@total_number_of_calls[method] += body.scan(/\b#{method}\b/).size
end
end
puts "Possibly uncalled methods"
@total_number_of_calls.each do |method, tally|
puts "- #{method}" if tally == 1
end
puts "Possibly private methods"
puts
@file_number_of_calls.each do |file, method_tallys|
puts "#{file}"
method_tallys.each do |method, tally|
puts "- #{method}" if @total_number_of_calls[method] == tally
end
puts
end
end
def extract_methods(file)
body = File.read(file)
methods = body.scan(/def\s+(?:\w+\.)?(\w+)/).flatten
methods.each do |method|
yield body, method
end
end
end
UsageReporter.new.report
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment