Skip to content

Instantly share code, notes, and snippets.

@kkchu791
Last active October 31, 2015 06:13
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 kkchu791/6cc027c326a6812f6880 to your computer and use it in GitHub Desktop.
Save kkchu791/6cc027c326a6812f6880 to your computer and use it in GitHub Desktop.
def word_count(sentence)
hash = sentence.downcase.gsub(/\W+/, ' ').split(" ").group_by{ |item| item }
hash.each { |item| hash[item[0]] = item[1].length }
end
p word_count("The quick brown fox jumped over the moon. This mammal did not jump over the sun; it jumped over the moon. And it was quick about it.")
@charliemcelfresh
Copy link

+1

@charliemcelfresh
Copy link

instead of this:

sentence.downcase.gsub(/\W+/, ' ').split(" ").group_by{ |item| item }

Some of our folks figured out this
sentence.downcase.split(/\W+/).group_by{ |item| item }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment