5/8 Iterating Over the Array

Hey everyone,

Thanks to those that have posted advice in the past, its been extremely helpful so far.

I’m having a bit of difficulty with hashes. The code reads my text, counts the words, but then gives me a syntax error. Can someone help me with this?

My code:

puts "Text please"
    text = gets.chomp
    words = text.split(" ")
    frequencies = Hash.new(0)
    words.each do
        |words| frequencies["word"] += 1
        puts frequencies["word"]

Where did I go wrong?

general syntax:

collection.each do |variable|

your |words| is on the wrong line, and i would put the hits into frequencies (frequencies[words]) not the string words

1 Like

well I didn’t use “do” so i’m not sure how to make that work, but this is the way I did it
text = gets.chomp
words = text.split
frequencies = Hash.new(0)
words.each {|word| frequencies[word] += 1}

Hey so i just had this problem too. I checked everything, changed everything. Nothing would work until I typed up a simple sentence into the console after it prompts me for text. Solved everything, I can finally move ahead!

1 Like