Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Count words

  • each
words = ["cat", "dog", "snake", "cat", "bug", "ant", "cat", "dog"]
count = {} of String => Int32

words.each { |word|
  if !count.has_key?(word)
    count[word] = 0
  end
  count[word] += 1
}

count.each { |word, cnt|
  puts "#{word} #{cnt}"
}