Skip to content

Instantly share code, notes, and snippets.

@lylo
Created January 6, 2015 11:38
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 lylo/bc2a9bb62402b45a7c55 to your computer and use it in GitHub Desktop.
Save lylo/bc2a9bb62402b45a7c55 to your computer and use it in GitHub Desktop.
NPS calc in Ruby
class NpsCalculator
def nps(scores)
(promotor_share(scores) - detractor_share(scores)).round
end
private
def promotor_share(scores)
scores.count {|score| score > 8} / scores.count.to_f * 100
end
def detractor_share(scores)
scores.count {|score| score < 7} / scores.count.to_f * 100
end
end
# Usage example
# scores = [9,5,10,6,4,5,7,8,8,9,6]
# puts NpsCalculator.new.nps scores
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment