Skip to content

Instantly share code, notes, and snippets.

@jaydonnell
Last active December 12, 2015 07:18
Show Gist options
  • Save jaydonnell/4735159 to your computer and use it in GitHub Desktop.
Save jaydonnell/4735159 to your computer and use it in GitHub Desktop.
ruby code
# let's count words
a = ['the quick brown fox jumped over the log', 'the slow brown fox ran fast', 'the blue fast cat jumped']
a.reduce({}) { |m, o|
o.split(' ').each{ |w|
m.key?(w) ? m[w] = m[w]+ 1 : m[w] = 1
}
m
}.sort { |a,b| b[1] <=> a[1] }
# => [["the", 4], ["brown", 2], ["fast", 2], ["jumped", 2], ["fox", 2], ["log", 1], ["slow", 1], ["ran", 1], ["over", 1], ["cat", 1], ["blue", 1], ["quick", 1]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment