5. Iterating over the array


I am not sure what is wrong with my code. My code works as I intended and I think it did what the instruction asked me to do. What am I doing wrong?

Instruction :

Use .each to iterate over the words array.For each word we find, assume that the word itself is a key in frequencies and increment its value by 1.

This is why our default is 0. The first time we find the word, it will have a default value of 0 that we can increment up to 1.

my code :

puts "Input your statement human"
text = gets.chomp
words = text.split(" ")
frequencies = Hash.new(0)
words.each do
    |word| frequencies["word"]+= 1
puts frequencies["word"]


New to this and just finished this lesson myself, but I think the issue is just the " " around word twice at the bottom, making it think it's a string? Looks great (to me) otherwise!


Hello, you need to remove the quotaion marks from word.

words.each do |word| frequencies[word] +=1
puts frequencies[word]


Okay well first of all,

This is all done correclty, however, you made a mistake that occurs for many people. You have the word "words" plural in some places, and singular in others. I hope that makes sense. What you should do is take out all the letter "s" in the word, "word."


This is not the error. The variable word is just a placeholder for each single word found within the sentence held in variable words. The problem is that the OP has written |word| frequencies["word"]+= 1 - with "word" instead of word, which makes the interpreter take the literal string value "word" instead of the string value contained in the variable word.


Hi everyone!
I am having an issue with this code.
Can someone tell please what would be the exact code to pass the lesson?
I tried everything I could think of, couldn't pass.

Thank you!


Can you please check what's wrong here:

puts "Input your statement human"
text = gets.chomp
words = text.split(" ")
frequencies = Hash.new(0)
words.each do
    |word| frequencies[word]+= 1
puts frequencies[word]

I am not sure what else to do to pass this lesson, have been working on it for a while.Maybe something wrong with the system.


You don't need that statement.


Thanks! It still doesn't let me pass, even if I remove the statement.


This weird: i replaces first 2 lines with
text= "L L L K K A A A B C"
and it worked!