Skip to content

Instantly share code, notes, and snippets.

Created March 3, 2012 20:12
Show Gist options
  • Save anonymous/1967974 to your computer and use it in GitHub Desktop.
Save anonymous/1967974 to your computer and use it in GitHub Desktop.
1.9.2p290 :006 > string
=> "doo bee doo bee"
1.9.2p290 :007 > string="doo bee doo bee"
=> "doo bee doo bee"
1.9.2p290 :008 > string=string.downcase.split
=> ["doo", "bee", "doo", "bee"]
1.9.2p290 :009 > string
=> ["doo", "bee", "doo", "bee"]
1.9.2p290 :010 > def count_w(list)
1.9.2p290 :011?> c=Hash.new(0)
1.9.2p290 :012?> for a_word in list
1.9.2p290 :013?> c[a_word]+=1
1.9.2p290 :014?> end
1.9.2p290 :015?> c
1.9.2p290 :016?> end
=> nil
1.9.2p290 :017 > count_w(string)
=> {"doo"=>2, "bee"=>2}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment