Skip to content

Instantly share code, notes, and snippets.

@kirhgoff
Created June 11, 2020 09:11
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 kirhgoff/ccf8af3d482f35168f1fbe773eca4b46 to your computer and use it in GitHub Desktop.
Save kirhgoff/ccf8af3d482f35168f1fbe773eca4b46 to your computer and use it in GitHub Desktop.
puts "Type something to count words in: "
frequencies = Hash.new(0)
text = gets.chomp.downcase!
text = text.gsub!(/[^A-Za-z ]/, '') #This removes punctuation
text.split
.each { |word| frequencies[word] += 1 }
.sort_by { |word, count| count }
.reverse
.each { |word, count| puts "#{word} #{count}" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment