Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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;; end
def freq; group_by {|e| e }.map {|k,v| [k,v.count] }.to_h; end
all_words =
.reject {|fn| fn =~ /_spec\.rb$/ }
.map {|fn|\w+/) }
method_set =
.select {|fst,_| fst == "def" }
.map {|_,method_name| method_name }
non_decl_names =
.reject {|fst,_| fst == "def" }
.map {|_,name| name }
freqs = {|t| method_set.include?(t) }
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
You can’t perform that action at this time.