Skip to content

Instantly share code, notes, and snippets.

@groteck
Created August 29, 2013 21:03
Show Gist options
  • Save groteck/6383382 to your computer and use it in GitHub Desktop.
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.)
#!/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