Advertisement
Guest User

Untitled

a guest
Mar 5th, 2012
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.27 KB | None | 0 0
  1. def count_words(string)
  2.   str = string.downcase
  3.   str = str.gsub(/\b/, '-')
  4.   words = str.scan(/-([a-z]+)-/)
  5.   count = Hash.new(0)
  6.   words.each do |word|
  7.     count[word] += 1
  8.     puts word
  9.   end
  10.   return count
  11. end
  12. c = count_words("This is a sentence")
  13. puts c, c["is"]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement