Skip to content

Instantly share code, notes, and snippets.

@aereal aereal/ambiguous.rb
Created Dec 5, 2012

Embed
What would you like to do?
#!/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
You can’t perform that action at this time.