-
-
Save corywheeler/95ee4498e4584c1b9fdb399f43566483 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' | |
require 'ap' | |
class Array | |
def to_h; Hash[self]; end | |
def to_s; 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_s | |
non_decl_names = | |
all_words.each_cons(2) | |
.reject {|fst,_| fst == "def" } | |
.map {|_,token| token } | |
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.flatten.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