Skip to content

Instantly share code, notes, and snippets.

@aereal
Created December 5, 2012 14:55
Show Gist options
  • Save aereal/4216115 to your computer and use it in GitHub Desktop.
Save aereal/4216115 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
LOOK = `which look 2>/dev/null`.strip
abort "#{$0} requires |look| executable command" unless File.executable?(LOOK)
def calculate_clarity(word)
candidates = `#{LOOK} -f #{word}`.strip.lines
$?.success? ? candidates.count : 0
end
require "ruby_parser"
target_file = ARGF
sexp_tree = Ruby19Parser.new.parse(target_file.read)
definition_nodes = [:class, :cdecl, :lasgn, :args]
define_names = definition_nodes.flat_map {|node| sexp_tree.enum_for(:each_of_type, node).map {|exp| exp[1] } }
define_clarities = define_names.map {|name| calculate_clarity(name) }
definitions = define_names.zip(define_clarities)
threshold = 20
too_ambiguous_names = definitions.select {|name, clarity| clarity >= threshold }
if $0 == __FILE__
too_ambiguous_names.each do |name, clarity|
puts "#{name} => #{clarity}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment