Last active
September 18, 2017 22:12
-
-
Save michaelfeathers/fb6d870a4144f7c85874dbc429b62a36 to your computer and use it in GitHub Desktop.
Find the number of calls in Ruby code that have happened no times, just once, or many times.
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 'set' | |
require 'find' | |
class Array | |
def to_h; Hash[self]; end | |
def to_set; Set.new(self); end | |
def freq; group_by {|e| e }.map {|k,v| [k,v.count] }.to_h; end | |
end | |
all_words = | |
Find.find(File.expand_path(ARGV[0])) | |
.grep(/\.rb$/) | |
.reject {|fn| fn =~ /_spec\.rb$/ } | |
.map {|fn| IO.read(fn).scan(/\w+/) } | |
.flatten | |
method_set = | |
all_words.each_cons(2) | |
.select {|fst,_| fst == "def" } | |
.map {|_,method_name| method_name } | |
.to_set | |
non_decl_names = | |
all_words.each_cons(2) | |
.reject {|fst,_| fst == "def" } | |
.map {|_,name| name } | |
freqs = | |
non_decl_names.select {|t| method_set.include?(t) } | |
.freq | |
.values | |
.freq | |
zeros = (method_set.to_a - non_decl_names.uniq).count | |
ones = freqs[1] | |
manys = freqs.values.reduce(:+) - ones | |
percentage = ones / (ones.to_f + manys) * 100.0 | |
puts "zeros: #{zeros}\nones: #{ones}\nmanys: #{manys}\npercentage: #{percentage}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment