Created
February 7, 2012 16:25
-
-
Save briandunn/1760540 to your computer and use it in GitHub Desktop.
Returns the location of the 10 longest methods. 1.9 only
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
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