Skip to content

Instantly share code, notes, and snippets.

@kerrizor
Forked from michaelfeathers/zom.rb
Created July 17, 2017 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kerrizor/786947c5859a7eadd8203c76f8723301 to your computer and use it in GitHub Desktop.
Save kerrizor/786947c5859a7eadd8203c76f8723301 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.
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