Skip to content

Instantly share code, notes, and snippets.

@briandunn
Created February 7, 2012 16:25
Show Gist options
  • Save briandunn/1760540 to your computer and use it in GitHub Desktop.
Save briandunn/1760540 to your computer and use it in GitHub Desktop.
Returns the location of the 10 longest methods. 1.9 only
require 'pathname'
require 'ripper'
module LongestMethod
class Method < Struct.new(:location, :size)
def self.add(line_number, token_count)
(@methods ||= []).push new(line_number, token_count)
end
def to_s
"size: #{size}\tlocation: #{location}"
end
def self.all
@methods
end
end
class Builder < Ripper::SexpBuilder
attr_reader :file_name
def initialize(file)
super(file.read)
@file_name = file.to_s
end
def on_def(defexp, argsexp, bodyexp) # parser event
Method.add(locator(defexp.last.first), bodyexp.flatten.compact.size)
end
def locator(line_num)
"#{file_name}:#{line_num}"
end
end
if ARGV.size == 1
file = Pathname.new(ARGV[0])
if file.directory?
for entry in Pathname::glob("#{file}/**/*.rb")
Builder.new(entry).parse
end
else
Builder.new(file).parse
end
puts Method.all.sort_by(&:size)[-10..-1]
else
puts "please supply a file or directory"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment