Created
February 21, 2011 01:56
-
-
Save michaelfeathers/836565 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# MF: Handle 'last method of class' case | |
FileName = ARGV[0] | |
MethodName = ARGV[1] | |
def numbered_lines(file_name) | |
lines = [] | |
File.open(file_name).each_line {|line| lines << line } | |
(0..(lines.size - 1)).zip(lines) | |
end | |
def method_size(numbered_lines) | |
return 0 if numbered_lines.empty? | |
numbered_lines.last[0] - numbered_lines.first[0] + 1 | |
end | |
class Array | |
def before_method_start | |
not(ident_fields[0] == 'def' && ident_fields[1] == MethodName) | |
end | |
def not_at_next_decl | |
(ident_fields[0] == 'def' && ident_fields[1] == MethodName) \ | |
|| ((not ident_fields[0] == 'def') && (not ident_fields[0] == 'class')) | |
end | |
def not_at_end | |
not ident_fields[0] == 'end' | |
end | |
private | |
def line | |
self[1] | |
end | |
def ident_fields | |
line.split(/\W/).select { |w| not w.empty? } | |
end | |
end | |
def method_text(file_name) | |
numbered_lines(file_name).drop_while(&:before_method_start).take_while(&:not_at_next_decl).reverse.drop_while(&:not_at_end).reverse | |
end | |
puts "" | |
text = method_text(FileName) | |
print text | |
puts "\n#{method_size(text)}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment