Created
August 29, 2013 21:03
-
-
Save groteck/6383382 to your computer and use it in GitHub Desktop.
Use .each to iterate over the words array. For each word we find, we'll want to make the word a key in the hash and increment its value by 1. (This is why our default is 0: when we first find the word, it will have a default value of 0 that we can increment up to 1.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# encoding: utf-8 | |
puts "introduzca palabras para contarlas: " | |
text = gets.chomp | |
words = text.split | |
frequencies = {} | |
words.each do |w| | |
frequencies.has_key?(w) ? frequencies[w] += 1 : frequencies[w] = 1 | |
end | |
p frequencies |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment